不调用GcmListenerService.onMessageReceived()


问题内容

我目前正在将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"
    ]
}

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);
        }
    }

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

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


问题答案:

正如这个
Github问题中所解释的,正是您的问题:

来自https://developers.google.com/cloud-
messaging/server#notifications_and_data_messages
“ GCM将代表客户端应用程序显示通知部分。
提供可选数据后,一旦用户单击通知,它将发送到客户端应用程序并打开客户端应用程序。[…]在Android上,数据有效载荷可以在用于启动你的活动的意图取回。

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

  • 将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用户单击通知后要在要打开的活动上添加意图过滤器时,使用与您在服务器端的“ 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>
  • 得到的意图的数据在你 的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);
        } 
    }

我已经对此进行了测试,可以确认它是否有效。(使用XMPP连接)