GCM通知未显示

3

我正在尝试使用特性,当Android收到推送消息后自动显示通知,如此处所述。
当我将notification字段添加到我的JSON负载中时,似乎通知不仅没有显示,而且onMessageReceived根本没有触发。 没有这个字段一切正常。
这是我的JSON负载:

{
 "to":"APA91bGIe59H1V5SsQkKvDOTgCTEaZWwgCJrdQ...",
 "message_id":"",
 "notification":{
                 "body":"Notification test",
                 "title":"Notification header",
                 "icon":"noti.png"
                },
 "data":{
         "test_message":"GCM test message!"
        }
}

所以,为什么会发生这种情况?或者没有自动通知?(如果是这样,notification字段有何用处...如果必须自己生成通知,我可以使用data字段)。 编辑:
最终成功了。图标字段应该像这样:"icon":"@drawable/noti"。 Google的文档错误真的很恼人。

你需要将通知放在数据对象内部。 - Prokash Sarkar
@Prokash 为什么文档说要使用单独的字段?无论如何,它都没有起作用。消息按预期传递,但没有通知。 - Le_Enot
@StenSoft 是的,我正在使用最新的 Google 示例 https://github.com/google/gcm - Le_Enot
"icon": "noti" 应按照文档描述正常工作: "指示通知图标。在 Android 上:将值设置为 myicon,对应 drawable 资源 myicon.png。" - morepork
如果您发布LogCat中显示的消息,那么这个问题将在之前被诊断出来。 - Jorgesys
@Elenasys 你说得对,但有时它表现很奇怪,如果我不手动重启adb的话。也许应该重新安装Android Studio。 - Le_Enot
1个回答

0
在我的情况下,通知没有被显示出来,因为我在构建通知时没有设置通知项。 以下构建器可行:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("GCM Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接