未调用GcmListenerService.onMessageReceived() [英] GcmListenerService.onMessageReceived() not called

查看:257
本文介绍了未调用GcmListenerService.onMessageReceived()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将GCM通知实施到我的应用中。

我遇到的问题是从中的 onMessageReceived()不调用GcmListenerService 实现。我从GCM服务器收到的数据很好,因为它会自动生成一个通知(我希望用我自己的通知,使用 onMessageReceived()方法)替换它,但是之后没有我的日志调用打印在日志中。



从服务器发送到GCM服务器的JSON

  {
通知:{
title:Title,
text:Message,
图标:@ drawable \ / ic_notification,
click_action:OPEN_MAIN_ACTIVITY
},
registration_ids:[
xxxx, xxxx,xxxx,etc
]
}

AndroidManifest.xml(仅限GCM部分)
$ b

 <! -  GCM START  - > 
< receiver
android:name =com.google.android.gms.gcm.GcmReceiver
android:exported =true
android:permission =com。 google.android.c2dm.permission.SEND>
< intent-filter>
< category android:name =com.my.package/>
< / intent-filter>
< / receiver>

< service
android:name =。Services.ListenerService
android:exported =false>
< intent-filter>
< / intent-filter>
< / service>

< service
android:name =。Services.IDListenerService
android:exported =false>
< intent-filter>
< / 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);


不知道请求令牌的方法是否相关,但如果需要,我可以发布。



如果问题的任何部分不清楚,请告诉我,我不是最擅长解释的。

b $ b

解决方案

所述Github问题正是您的问题:



https: //developers.google.com/cloud-messaging/server#notifications_and_data_messages
GCM将代表客户端应用程序显示通知部分。提供可选数据时,会将其发送到客户端应用程序一旦用户点击通知并打开客户端应用程序
[...]在Android上,可以在用于启动您的活动的Intent中检索数据有效载荷。

b
$ b

因此,数据是以用于启动的意图传递的h用户点击通知后的活动。
这意味着您需要执行以下操作:


  • 将click_action添加到您从服务器发送的通知密钥:
    例如


    $ b $ pre $ send_queue.append({'to':REGISTRATION_ID,
    'message_id':random_id(),
    通知:{
    body:Hello from Server!发生了什么事情?似乎可以工作!!!,
    title:Hello from Server!,
    icon:@ drawable / ic_school_white_48dp,
    sound:default,
    color:#03A9F4,
    click_action:OPEN_MAIN_ACTIVITY
    $,$ b $'data':{'message':Hello}})




请参阅 https://developers.google.com/cloud-messaging/server-ref#notification-payload-support




  • AndroidManifest.xml 中添加一个intent过滤器t在用户点击通知后打开,并在服务器端使用与click_action键相同的动作名称,例如:

     < activity 
    android:name =.ii.MainActivity
    android:label =@ string / title_activity_main>
    < intent-filter>
    < category android:name =android.intent.category.DEFAULT/>
    < / intent-filter>
    < / activity>


  • 从您的 onCreate() onNewIntent()的方法,如果您已将launchMode设置为singleTop,以便单击通知时要启动的活动,例如:

      @Override 
    保护无效的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);


    code $

    $ b $ p

    我已经测试过这个,可以确认它是否有效。 (使用XMPP连接)

    I'm currently working on implementing GCM notifications into my app.

    The problem that I'm having is that the onMessageReceived() method from my GcmListenerService implementation isn't called. 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.

    JSON that is sent from server to 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 a quick print to see if its called at 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 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:

    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."

    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: e.g.

      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

    • 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, 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 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, 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 this and can confirm that it works. (using XMPP connection)

    这篇关于未调用GcmListenerService.onMessageReceived()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆