java - GcmListenerService.onMessageReceived() not called -
i'm working on implementing gcm notifications app.
the problem i'm having onmessagereceived()
method gcmlistenerservice
implementation isn't called. receive data gcm servers fine, since automatically generates notification (i wish replace own notification using onmessagereceived()
method) after none of log calls printed in log.
json sent server gcm server
{ "notification" : { "title" : "title", "text" : "message", "icon" : "@drawable\/ic_notification", "click_action" : "open_main_activity" }, "registration_ids":[ "xxxx", "xxxx", "xxxx", "etc" ] }
androidmanifest.xml (gcm part only)
<!-- 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 (just quick print see if called @ all)
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); } }
not sure if method request tokens relevant, can post if needed.
if part of question unclear, let me know, i'm not best @ explaining.
as explained in this github issue problem:
from https://developers.google.com/cloud-messaging/server#notifications_and_data_messages "gcm display notification part on client app’s behalf. when optional data provided, sent client app once user clicks on notification , opens client app. [...] on android, data payload can retrieved in intent used launch activity."
so, data passed in intent used launch activity, after user taps on notification. means need following:
add click_action notification key send server: e.g.
send_queue.append({'to': registration_id, 'message_id': random_id(), "notification" : { "body" : "hello server! going on? seems work!!!", "title" : "hello server!", "icon" : "@drawable/ic_school_white_48dp", "sound": "default", "color": "#03a9f4", "click_action": "open_main_activity" }, 'data': { 'message': "hello" }})
see reference notification payload at: https://developers.google.com/cloud-messaging/server-ref#notification-payload-support
in
androidmanifest.xml
add intent filter on activity want opened once user clicks on notification, same action name used on "click_action" key on server side, e.g:<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>
get data intent on oncreate() method or on onnewintent() if you've set launchmode singletop activity want launch when notification clicked, e.g:
@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); } }
i've tested , can confirm works. (using xmpp connection)
Comments
Post a Comment