我目前正致力于在我的应用中实现 GCM 通知。
我遇到的问题是未调用我的 GcmListenerService
实现中的 onMessageReceived()
方法。我从 GCM 服务器收到数据很好,因为它会自动生成一个通知(我希望使用 onMessageReceived()
方法将其替换为我自己的通知)但之后我的日志调用都没有被打印出来在日志中。
从服务器发送到 GCM 服务器的 JSON
{
"notification" : {
"title" : "Title",
"text" : "Message",
"icon" : "@drawable\/ic_notification",
"click_action" : "OPEN_MAIN_ACTIVITY"
},
"registration_ids":[
"xxxx", "xxxx", "xxxx", "etc"
]
}
AndroidManifest.xml(仅 GCM 部分)
<!-- GCM START -->
<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="com.my.package" />
</intent-filter>
</receiver>
<service
android:name=".Services.ListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".Services.IDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- GCM END -->
GcmListenerService(只是快速打印以查看它是否被调用)
public class ListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("title");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
}
}
不确定请求 token 的方法是否相关,但如果需要我可以发布。
如果问题的任何部分不清楚,请告诉我,我不是最擅长解释的。
最佳答案
如 this 中所述Github 问题正是您的问题:
来自 https://developers.google.com/cloud-messaging/server#notifications_and_data_messages “GCM 将代表客户端应用程序显示通知部分。提供可选数据时,一旦用户单击通知并打开客户端应用程序,它就会发送到客户端应用程序。 [...] 在 Android 上,可以在用于启动 Activity 的 Intent 中检索数据负载。”
因此,数据在用于启动 Activity 的 Intent 中传递,在用户点击通知后。 这意味着您需要执行以下操作:
向您从服务器发送的通知键添加一个 click_action: 例如
send_queue.append({'to': REGISTRATION_ID, 'message_id': random_id(), "notification" : { "body" : "Hello from Server! What is going on? Seems to work!!!", "title" : "Hello from Server!", "icon" : "@drawable/ic_school_white_48dp", "sound": "default", "color": "#03A9F4", "click_action": "OPEN_MAIN_ACTIVITY" }, 'data': { 'message': "Hello" }})
请参阅通知负载引用:https://developers.google.com/cloud-messaging/server-ref#notification-payload-support
在
AndroidManifest.xml
中,为您希望在用户点击通知后打开的 Activity 添加一个 Intent 过滤器,其 Action 名称与您在“click_action”键上使用的名称相同在服务器端,例如:<activity android:name=".ui.MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="OPEN_MAIN_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
如果您已设置单击通知时要启动的 Activity 的 launchMode 为 singleTop,例如:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.hasExtra(Constants.KEY_MESSAGE_TXT)) { String message = intent.getStringExtra(Constants.KEY_MESSAGE_TXT); Log.d(TAG, message); } }
我已经对此进行了测试,可以确认它是否有效。 (使用 XMPP 连接)
关于java - GcmListenerService.onMessageReceived() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31178029/