简体   繁体   English

未调用GcmListenerService.onMessageReceived()

[英]GcmListenerService.onMessageReceived() not called

I'm currently working on implementing GCM notifications into my app. 我目前正致力于将GCM通知实施到我的应用中。

The problem that I'm having is that the onMessageReceived() method from my GcmListenerService implementation isn't called. 我遇到的问题是我的GcmListenerService实现中的onMessageReceived()方法没有被调用。 I receive the data from the GCM servers fine, since it automatically generates a notification (I wish to replace this with my own notification using the onMessageReceived() method) but after that none of my log calls are printed in the log. 我很好地从GCM服务器接收数据,因为它会自动生成通知(我希望使用onMessageReceived()方法将其替换为我自己的通知),但之后我的日志调用都没有打印在日志中。

JSON that is sent from server to GCM server 从服务器发送到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 part only) 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 (just a quick print to see if its called at all) 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);
    }
}

Not sure if the method to request tokens is relevant, but I can post it if needed. 不确定请求令牌的方法是否相关,但我可以在需要时发布。

If any part of the question is unclear, let me know, I'm not the best at explaining. 如果问题的任何部分不清楚,请告诉我,我不是最好的解释。

As explained in this Github issue which is exactly your problem: 正如在这个 Github问题中所解释的那样, 正是你的问题:

From https://developers.google.com/cloud-messaging/server#notifications_and_data_messages "GCM will display the notification part on the client app's behalf. When optional data is provided, it is sent to the client app once user clicks on the notification and opens the client app. [...] On Android, data payload can be retrieved in the Intent used to launch your activity. " 来自https://developers.google.com/cloud-messaging/server#notifications_and_data_messages“GCM将代表客户端应用程序显示通知部分。 提供可选数据后,一旦用户点击通知,就会将其发送到客户端应用程序并打开客户端应用程序。[...]在Android上,可以在用于启动活动的Intent中检索数据有效负载。

So, the data is passed in the intent used to launch the activity, after the user taps on the notification . 因此,在用户点击通知后 ,数据将以用于启动活动的意图传递。 This means you need to do the following: 这意味着您需要执行以下操作:

  • Add a click_action to the notification key you send from the server: eg 将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" }}) 

See the reference for notification payload at: https://developers.google.com/cloud-messaging/server-ref#notification-payload-support 请参阅https://developers.google.com/cloud-messaging/server-ref#notification-payload-support上的通知有效负载参考

  • In AndroidManifest.xml add an intent filter on the activity you want to be opened once the user clicks on the notification, with the same action name you used on the "click_action" key on the server side, eg: 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> 
  • Get the data from the intent on your onCreate() method or on onNewIntent() if you've set the launchMode to singleTop for the activity you want to launch when the notification is clicked, eg: 如果您在单击通知时将launchMode设置为要启动的活动的singleTop,则从onCreate()方法 onNewIntent()获取意图数据,例如:

     @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 this and can confirm that it works. 我已对此进行了测试并确认其有效。 (using XMPP connection) (使用XMPP连接)

如果下游消息(json)包含通知,则不会调用GcmListenerService.onMessageReceived()。

To receive the message in onMessageReceived you need to define the top level "data" field in your message object. 要在onMessageReceived中接收消息,您需要在消息对象中定义顶级“数据”字段。 The notification field is handled automatically and generates a notification, onMessageReceived does not get passed any of the data in the notification field. 通知字段自动处理并生成通知,onMessageReceived不会传递通知字段中的任何数据。

Update your message object to include a data field and onMessageReceived should be called: 更新您的消息对象以包含数据字段,并应调用onMessageReceived:

{
    "notification" : {
        "title" : "Title",
        "text" : "Message",
        "icon" : "@drawable\/ic_notification",
        "click_action" : "OPEN_MAIN_ACTIVITY"
    },
    "data": {
        "some_key": "some_value"
    },
    "registration_ids":[
        "xxxx", "xxxx", "xxxx", "etc"
    ]
}

onMessageReceived只调用“仅数据”推送按摩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM