Dan*_*Dan 14 java android google-cloud-messaging
我目前正致力于将GCM通知实施到我的应用中.
我遇到的问题是onMessageReceived()我的GcmListenerService实现方法没有被调用.我很好地从GCM服务器接收数据,因为它会自动生成通知(我希望使用该onMessageReceived()方法将其替换为我自己的通知),但之后我的日志调用都没有打印在日志中.
从服务器发送到GCM服务器的JSON
{
"notification" : {
"title" : "Title",
"text" : "Message",
"icon" : "@drawable\/ic_notification",
"click_action" : "OPEN_MAIN_ACTIVITY"
},
"registration_ids":[
"xxxx", "xxxx", "xxxx", "etc"
]
}
Run Code Online (Sandbox Code Playgroud)
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 -->
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
不确定请求令牌的方法是否相关,但我可以在需要时发布.
如果问题的任何部分不清楚,请告诉我,我不是最好的解释.
bit*_*tek 12
正如在这个 Github问题中所解释的那样,这正是你的问题:
来自https://developers.google.com/cloud-messaging/server#notifications_and_data_messages"GCM 将代表客户端应用程序显示通知部分.提供可选数据后,一旦用户点击通知,就会将其发送到客户端应用程序并打开客户端应用程序.[...]在Android上,可以在用于启动活动的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" }})
Run Code Online (Sandbox Code Playgroud)请参阅https://developers.google.com/cloud-messaging/server-ref#notification-payload-support上的通知有效负载参考
在AndroidManifest.xml用户单击通知后,您希望打开的活动添加一个intent过滤器,使用您在服务器端的"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>
Run Code Online (Sandbox Code Playgroud)得到你的意图的数据的onCreate()方法或在onNewIntent() ,如果你已经设置的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);
}
}
Run Code Online (Sandbox Code Playgroud)我已对此进行了测试并确认其有效.(使用XMPP连接)
| 归档时间: |
|
| 查看次数: |
11584 次 |
| 最近记录: |