Parse.com推送通知不显示在android上的背景
我使用Parse.com与Ionic应用程序和PushPlugin并试图通过GCM与自定义发件人ID实现Parse的推送通知。Parse.com推送通知不显示在android上的背景
当我向所有设备发送消息或使用带有cURL的REST API时,iOS通知在应用程序处于后台时触发正常,但android通知不适用。
这就是我与其他API第一次尝试针对iOS,这是正常工作:
curl -X POST \
-H "X-Parse-Application-Id: APP-ID" \
-H "X-Parse-REST-API-Key: API-KEY" \
-H "Content-Type: application/json" \
-d '{
"where": {
"deviceType": "ios"
},
"data": {
"alert": "Hello World!"
}
}' \
https://api.parse.com/1/push
iOS的通知接收即使该应用程序被关闭或开放的背景显示。
现在,当我尝试同样瞄准Android设备:
curl -X POST \
-H "X-Parse-Application-Id: APP-ID" \
-H "X-Parse-REST-API-Key: API-KEY" \
-H "Content-Type: application/json" \
-d '{
"where": {
"deviceType": "android"
},
"data": {
"alert": "Hello World!"
}
}' \
https://api.parse.com/1/push
该通知是由设备接收和adb logcat
部分记录,但在通知栏或不显示其它方法确认。我尝试将'alert'
更改为'message'
,但这没有效果。
但是,如果我尝试使用与邮差或卷曲的GCM HTTP API,一切正常:
curl 'https://android.googleapis.com/gcm/send' \
-H 'authorization: key=API-KEY' \
-H 'content-type: application/json' \
-d '{
"registration_ids" : [
"DEVICE-REGISTRATION-ID"
],
"data" : {
"message": "You Go I Go, Buddy!"
}
}'
当登录adb logcat
日志使用GCM API和解析的推送通知API时是不同的:
随着解析:
I/GCM (10319): GCM message co.yougoigo.mobile 0:1423318254669687%bd9ff524f9fd7ecd
V/GCMBroadcastReceiver(11506): onReceive: com.google.android.c2dm.intent.RECEIVE
V/GCMBroadcastReceiver(11506): GCM IntentService class: com.plugin.gcm.GCMIntentService
V/GCMBaseIntentService(11506): Acquiring wakelock
V/GCMBaseIntentService(11506): Intent service name: GCMIntentService-GCMIntentService-3
D/GCMIntentService(11506): onMessage - context: [email protected]
而且随着GCM卷曲电话:
I/GCM (10319): GCM message co.yougoigo.mobile 0:1423318321652064%bd9ff524f9fd7ecd
I/ActivityManager( 745): Start proc co.yougoigo.mobile for broadcast co.yougoigo.mobile/com.plugin.gcm.CordovaGCMBroadcastReceiver: pid=11788 uid=10187 gids={50187, 9997, 3003} abi=armeabi-v7a
V/GCMBroadcastReceiver(11788): onReceive: com.google.android.c2dm.intent.RECEIVE
V/GCMRegistrar(11788): Setting the name of retry receiver class to com.plugin.gcm.CordovaGCMBroadcastReceiver
V/GCMBroadcastReceiver(11788): GCM IntentService class: com.plugin.gcm.GCMIntentService
V/GCMBaseIntentService(11788): Acquiring wakelock
V/GCMBaseIntentService(11788): Intent service name: GCMIntentService-GCMIntentService-1
D/GCMIntentService(11788): onMessage - context: [email protected]
E/GCMIntentService(11788): Number format exception - Error parsing Notification ID: Invalid int: "null"
V/GCMBaseIntentService(11788): Releasing wakelock
目前的清单如下:
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="4" android:versionName="0.0.4" package="co.yougoigo.mobile" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="CordovaApp" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:exported="true" android:name="com.plugin.gcm.PushHandlerActivity" />
<receiver android:name="com.plugin.gcm.CordovaGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="co.yougoigo.mobile" />
</intent-filter>
</receiver>
<service android:name="com.plugin.gcm.GCMIntentService" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/fb_app_id" />
<activity android:label="@string/fb_app_name" android:name="com.facebook.LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="co.yougoigo.mobile.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="co.yougoigo.mobile.permission.C2D_MESSAGE" />
</manifest>
有谁有越来越GCM通知上的PhoneGap应用工作的经验吗?
为了使用带有Parse Push的phonegap/cordova PushPlugin,您必须认识到Parse实际上是以非标准方式包装它的内容。这会导致PushPlugin在应用程序处于前台时接收推送通知,而不会在应用程序处于后台时收到推送通知。从GCMIntentService.java
@Override
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null)
{
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
}
else {
extras.putBoolean("foreground", false);
// Send a notification if there is a message
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
}
}
}
至于当PushPlugin.isInForeground()
校验失败它检查“消息”财产上的额外捆绑你可以看到
源代码段。
2观察:
- 解析不包括在通知“消息”,使PushPlugin不调用
createNotification(content, extras)
- 如果你看它也包装捆扎的“有效载荷”属性它比“传统的”GCM通知更深一层,通过将通知放置在具有关键“数据”的对象上。
这里的包是什么样子的JSON
{ ...
"payload" : {
"data" : {
"message" : "your message here",
"title": "your title here",
...,
"customData": { ... }
}
}
因此,您必须做一些自定义逻辑从包extras.getString("payload")
提取有效载荷,然后在这里创建一个新的JSONObject(文档:http://developer.android.com/reference/org/json/JSONObject.html )。
取代检查bundle.getString("message")
您需要检查是否存在payload.data,然后解析出消息和标题,并确保它们位于createNotification函数的正确位置,以便重新打包该包(或创建你自己)。
我的建议是保留原有代码,但对分析格式化通知单独检查并调用自定义createNotification函数。
如:
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
} else if (extras.getString("message") && extras.getString("message").length() != 0) {
extras.putBoolean("foreground", false);
createNotification(context, extras);
} else {
JSONObject payload = extras.getString("payload");
JSONObject data = payload.getJSONObject("data");
String message = data.getString("alert") //parse puts the message as an alert if you don't use custom json payload
extras.putString("message", alert)
createNotification(context, extras)
}
希望这有助于。老实说,我希望解析像其他人一样发送数据,以便它与开箱即用的PushPlugin兼容。对于PushPlugin GCMIntentService.java
的源代码发现这里(官方GitHub库):https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/GCMIntentService.java
这太棒了@ james-gilchrist!我的Android开发经验甚少,不知道从哪里开始解决这个问题。 – BarakChamo 2015-02-28 15:19:00
不客气。 – 2015-03-01 18:37:37
我有完全相同的体验。这有什么好运?你如何在解析中设置你的安装? – 2015-02-17 22:17:25
通知未显示的原因是因为Parse会将android通知发送到其他自定义接收器。你必须使用像https://github.com/avivais/phonegap-parse-plugin这样的东西来包含Parse SDK,但是对于我来说插件会中断。我最终做的是直接通过GCM发送android推送通知,而不是Parse Push。 – BarakChamo 2015-02-18 10:07:47
这是我的怀疑。我也试图使用相同的分析插件,但每次打开时都会崩溃。我猜这意味着你没有为iOS开发。我正在寻找一个好的混合解决方案。 – 2015-02-18 19:43:49