IBM Mobilefirst 7.1 PushNotifications:无法从后台检索通知文本到前台

Posted

技术标签:

【中文标题】IBM Mobilefirst 7.1 PushNotifications:无法从后台检索通知文本到前台【英文标题】:IBM Mobilefirst 7.1 PushNotifications: Unable to retrieve notification text from background to foreground 【发布时间】:2016-01-27 20:21:15 【问题描述】:

我们在 MobileFirst 应用程序中集成了基于标签的通知和广播通知。当应用程序打开并且用户能够看到通知文本时,我们能够发送通知。但是当用户杀死应用程序并且通知到达通知面板时:用户能够单击通知并打开应用程序但它不显示通知文本。

请在下面找到代码:

/* TagBased and Broadcast Notification Code Start */
var chkHolder;
function suborunsub(chkbxFlag)
console.log('The value of checkbox flag is:'+chkbxFlag);
var checkBox;
if(chkbxFlag=='TG1') 
    chkHolder = "TAG1";
    checkBox = document.getElementById("SUBTG1").checked;
    console.log('The checkbox value is:'+checkBox);
    if(checkBox) 
        subscribeToNotifTag('TAG1');
     else 
        unsubscribeFromNotifTag('TAG1');
    
 else if(chkbxFlag=='TG2') 
    chkHolder = "TAG 2";
    checkBox = document.getElementById("SUBTG2").checked;
    console.log('The checkbox value is:'+checkBox);
    if(checkBox) 

        subscribeToNotifTag('TAG2');
     else 
        unsubscribeFromNotifTag('TAG2');
    
 
//---------------------------- Check if push support is available-----------
function isPushSupported() 
var isSupported = false;
if (WL.Client.Push)
    isSupported = WL.Client.Push.isPushSupported();
   
WL.SimpleDialog.show("Alert", JSON.stringify(isSupported), [ 
    text : 'Close',
    handler : function() 
  
]);

// --------------------------------- Subscribe to tag --------------------

function subscribeToNotifTag(nTag) 
  WL.Client.Push.subscribeTag(nTag, 
        onSuccess: subscribeTagSuccess,
        onFailure: subscribeTagFailure
    );


function subscribeTagSuccess() 
    WL.SimpleDialog.show("Alert", "Subscribed to : "+chkHolder, [ 
    text : 'Close',
    handler : function() 
  
  ]);


function subscribeTagFailure() 
WL.SimpleDialog.show("Alert", "Failed to subscribe : "+chkHolder, [ 
    text : 'Close',
    handler : function() 
  
  ]);


// ------------------------------- Unsubscribe from tag --------------------

function unsubscribeFromNotifTag(nTag) 
WL.Client.Push.unsubscribeTag(nTag, 
        onSuccess: unsubscribeTagSuccess,
        onFailure: unsubscribeTagFailure
    );

function unsubscribeTagSuccess() 
WL.SimpleDialog.show("Alert", "Unsubscribed to : "+chkHolder, [ 
    text : 'Close',
    handler : function() 
  
  ]);


function unsubscribeTagFailure() 
WL.SimpleDialog.show("Alert", "Failed to unsubscribe : "+chkHolder, [ 
    text : 'Close',
    handler : function() 
  
  ]);


//------------------------------- Handle received notification -----------
WL.Client.Push.onMessage = function (props) 
if(WL.StaticAppProps.ENVIRONMENT=='iphone') 
WL.SimpleDialog.show("Alert", ""  + JSON.stringify(props.alert.body), [ 
    text : 'Close',
    handler : function() 
]);
else if(WL.StaticAppProps.ENVIRONMENT=='android') 
    WL.SimpleDialog.show("Alert", ""  + JSON.stringify(props.alert), [ 
        text : 'Close',
        handler : function() 
    ]);
    
;
/* TagBased and Broadcast Notification Code End */

AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myAPP" android:versionCode="6" android:versionName="8.2">  
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21"/>  
<supports-screens android:smallScreens="false" android:normalScreens="true" android:largeScreens="false"/>  
<uses-permission android:name="android.permission.INTERNET"/>  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
<!-- Push permissions -->  
<permission android:name="com.myAPP.permission.C2D_MESSAGE" android:protectionLevel="signature"/>  
<uses-permission android:name="com.myAPP.permission.C2D_MESSAGE"/>  
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>  
<uses-permission android:name="android.permission.WAKE_LOCK"/>  
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>  
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
<uses-permission android:name="android.permission.READ_CALENDAR"/>  
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>  
<application android:label="@string/app_name"  android:icon="@drawable/icon"> 
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBHvtzEPwG4DYTG9Zsvijz9mRIC53ehleU"/>  
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>  
    <activity android:name=".myAPP" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|screenSize" android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:screenOrientation="portrait"> 
        <intent-filter> 
            <action android:name="android.intent.action.MAIN"/>  
            <category android:name="android.intent.category.LAUNCHER"/> 
        </intent-filter>  
        <intent-filter> 
            <action android:name="com.myAPP.myAPP.NOTIFICATION"/>  
            <category android:name="android.intent.category.DEFAULT"/> 
        </intent-filter> 
    </activity>  
    <activity android:name=".MGMapFragment" android:theme="@style/Theme.MGMapTheme"/>  
    <activity android:name=".MGMultiMapFragment" android:theme="@style/Theme.MGMapTheme"/>  
    <activity android:name=".MGPickerMapFragment" android:theme="@style/Theme.MGPckrTheme"/>  
    <!-- Preference Activity  -->  
    <activity android:name="com.worklight.common.WLSettingActivity" android:label="Worklight Settings"/>  
    <!-- UI Activity for displaying native dialogs  -->  
    <activity android:name="com.worklight.wlclient.ui.UIActivity"/>  
    <!-- Push service  -->  
    <!-- In order to use the c2dm library, an application must declare a class with the name C2DMReceiver, in its own package, extending com.google.android.c2dm.C2DMBaseReceiver 
        It must also include this section in the manifest, replacing "com.google.android.apps.chrometophone" with its package name. -->  
    <service android:name=".GCMIntentService"/>  
    <service android:name=".ForegroundService"/>  
    <!-- Only google service can send data messages for the app. If permission is not set - any other app can generate it -->  
    <receiver android:name="com.worklight.androidgap.push.WLBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> 
        <!-- Receive the actual message -->  
        <intent-filter> 
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>  
            <category android:name="com.myAPP"/> 
        </intent-filter>  
        <!-- Receive the registration id -->  
        <intent-filter> 
            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>  
            <category android:name="com.myAPP"/> 
        </intent-filter> 
    </receiver> 
   </application> 
 </manifest>

【问题讨论】:

应用无论是关闭还是打开,都到达main.js文件中相同的代码路径;所以问题是设置问题。可能在 AndroidManifest.xml - 你可以添加它吗?你有没有以任何方式改变它?字符串?名字? 应用程序已关闭,我们没有更改清单文件中的任何字符串或名称。 提及您的确切内部版本号。您是否检查了示例应用程序?效果不是很好吗? 内部版本号:7.1.0.0-MFPF-IF201512191535。我已经尝试过运行良好的示例应用程序。即使我已经比较了这两个 android manifest.xml 文件,除了一些额外的活动之外没有任何区别。 删除代码中的“WL.StaticAppProps.ENVIRONMENT”条件检查,然后尝试。 【参考方案1】:

我已尝试使用示例应用程序附带的默认代码,它运行良好。但是使用这个“WL.StaticAppProps.ENVIRONMENT”的原因是我们只想向用户显示通知文本。有没有最好的方式只向用户显示通知文本?

“只是通知文本”是什么意思?这与默认通知有何不同?示例应用程序展示了如何访问通知对象并显示有效负载和道具。分开。

function pushNotificationReceived(props, payload) 

    WL.SimpleDialog.show("Push Notifications", "Provider notification data: " + JSON.stringify(props), [ 
        text : 'Close',
        handler : function() 
            WL.SimpleDialog.show("Push Notifications", "Application notification data: " + JSON.stringify(payload), [ 
                text : 'Close',
                handler : function() 
              ]);      
        
    ]);

【讨论】:

当我们使用“+ JSON.stringify(props)”时,它会将通知发送为“Provider notification data: "alert":test, "badge":1,"sound":null,"优先级“:0”。所以在这里我们只想向用户显示“警报”:测试。所以这就是我们将它定制为“+ JSON.stringify(props.alert)”的地方。

以上是关于IBM Mobilefirst 7.1 PushNotifications:无法从后台检索通知文本到前台的主要内容,如果未能解决你的问题,请参考以下文章

IBM MobileFirst 7.1 中生成密钥库的位置

IBM MobileFirst 服务器 7.1 PushNotification 订阅者 ID(USERID) 未存储

IBM Mobilefirst 7.1 PushNotifications:无法从后台检索通知文本到前台

如何使用 IBM MobileFirst 7.1 实现文件下载?

IBM MobileFirst 7.1 - 本地服务器复制资源

在 Xcode 中运行时,IBM MobileFirst 7.1 混合应用程序构建失败