我正在将Parse.com与Ionic应用程序和PushPlugin一起使用,并尝试使用自定义发件人ID通过GCM实施Parse的推送通知。
当我向所有设备发送消息或使用带有cURL的REST API时, 当应用程序在后台但没有安装android通知时,iOS通知会触发。
这是我尝试使用其余的API First for 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'
,但这没有效果。
但是,如果我尝试使用Postman或cURL的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!"
}
}'
使用GCM API和Parse推送通知API时,记录adb logcat
时日志不同:
使用Parse:
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: android.app.Application@251ee33b
使用GCM cURL致电:
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: android.app.Application@251ee33b
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通知用于手机应用程序的经验?
答案 0 :(得分:4)
为了使用带有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()
检查失败时,它会检查附加软件包上的'message'属性。
2观察:
createNotification(content, extras)
这是捆绑在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 repo):https://github.com/phonegap-build/PushPlugin/blob/master/src/android/com/plugin/gcm/GCMIntentService.java