GCM 3.0 - gcm不会自动显示带有通知参数的通知

6
新的GCM 3.0版本应该能够自动显示从服务器发送的通知,如果它们包含“notification”参数。
如文档中所述:(链接)

预定义选项的通知参数表示,如果客户端应用程序在Android上实现了GCMListenerService,则GCM将代表客户端应用程序显示消息。

然而,即使已经实现了“GCMListenerService”,我仍然无法让它正常工作。
AndroidManifest.xml
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="cz.kubaspatny.pushservertest" />
        </intent-filter>
    </receiver>

    <service
        android:name="cz.kubaspatny.pushservertest.gcm.CustomGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

CustomGcmListenerService.java

public class CustomGcmListenerService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle extras) {
        super.onMessageReceived(from, extras);
        Log.d("GcmListenerService", "Received gcm from " + from + " with bundle " + extras.toString());
    }
}

服务器发出的通知已经被记录,但GCM没有显示。

Received gcm from 333813590000 with bundle Bundle[{notification={"icon":"ic_launcher.png","body":"great match!","title":"Portugal vs. Denmark"}, collapse_key=do_not_collapse}]

来自服务器的消息:

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

是否还需要进行其他操作来实现自动显示?


你在“notification”负载中发送了什么? - shkschneider
@shkschneider编辑了问题。但我正在发送“标题”,“正文”和“图标”。 - Kuba Spatny
“Automatic display” 是什么意思? - injecteer
在 I/O 15 上,@injecteer 表示如果 GCM 包含特定信息(标题、图标、文本),它将自行显示通知。或者如文档中所述,如果客户端应用程序实现了 GCMListenerService,则 GCM 将代表客户端应用程序显示消息。 - Kuba Spatny
也许在即将发布的SDK版本中? - injecteer
@injecteer 我不这么认为,它应该是Google Play服务7.5的一部分。请参阅博客文章 - Kuba Spatny
1个回答

2
尝试将通知字段设置为数据字段的同级。数据字段传递给onMessageReceived,而通知字段用于自动生成通知。
{       
      "registration_ids":[...],
      "notification" : {
            "body" : "great match!",
            "icon" : "ic_launcher.png",
            "title" : "Portugal vs. Denmark"
      }

}

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