是否应该通过Android显示到达的GCM消息?

时间:2016-07-07 05:21:48

标签: android google-cloud-messaging node-gcm

问题是我的GCM消息到达我的应用程序,但没有(自动)显示在屏幕上。

07-06 21:33:11.525 11269-11269/com.example.myapp D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp cmp=com.example.myapp/com.google.android.gms.gcm.GcmReceiver (has extras) }, receiver=com.google.android.gms.gcm.GcmReceiver@3a59595e
07-06 21:33:11.534 11269-11269/com.example.myapp D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) } packageName=com.example.myapp resultCode=-1 resultData=null resultExtras=null}
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Calling onStartCommand: com.example.myapp.MyGcmListenerService@bc04a0c, flags=0, startId=1
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@df9e13f className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null}
07-06 21:33:11.539 11269-11269/com.example.myapp D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@df9e13f className=com.example.myapp.MyGcmListenerService packageName=com.example.myapp intent=null}
07-06 21:33:11.543 11269-11269/com.example.myapp D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@df9e13f startId=1 args=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.example.myapp (has extras) }}07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-Destroying service: com.example.myapp.MyGcmListenerService@bc04a0c
07-06 21:33:11.565 11269-11269/com.example.myapp D/ActivityThread: SVC-STOP_SERVICE handled : 0 / android.os.BinderProxy@df9e13f
07-07 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: bundle: Bundle[{key1=message1, key2=message2, notification=Bundle[{e=1, body=This is a notification that will be displayed ASAP., icon=ic_launcher, title=Hello, World}], collapse_key=com.example.myapp}]
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: From: 28REDACTED98
07-06 21:33:11.564 11269-11530/com.example.myapp D/MyGcmListenerService: Message: null

我收到的代码是:

public class MyGcmListenerService extends GcmListenerService {
    public static final String TAG = MyGcmListenerService.class.getSimpleName();

    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.d(TAG, "bundle: " + data);
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        ...

发送代码(使用node-gcm)是:

  var message = new ngcm.Message({
        priority: 'high',
        contentAvailable: true,
        restrictedPackageName: "com.example.myapp",
        data: {
                key1: 'message1',
                key2: 'message2'
        },
        notification: {
                title: "Hello, World",
                icon: "ic_launcher",
                body: "This is a notification that will be displayed ASAP."
        }
  });

我曾尝试使用contentAvailablerestrictedPackage,但没有区别。

我的问题是:

  1. 我的接收代码是否需要在消息到达时创建屏幕通知,或者应该由Android为我做这个(我相信)?
  2. 示例代码说捆绑包应该包含data.getString("message");,但我的应用程序将此视为空。为什么?

1 个答案:

答案 0 :(得分:1)

要自动显示gcm通知,您的推送消息json格式应与此相同

"notification" : {
            "body" : "great match!",
            "icon" : "ic_launcher.png",
            "title" : "Portugal vs. Denmark"
                }

有关进一步说明,您可以看到此链接
 Android GCM Notifications