当前位置: 首页 > 面试题库 >

不调用GcmListenerService.onMessageReceived()

董嘉祯
2023-03-14
问题内容

我目前正在将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 html" target="_blank">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连接)



 类似资料:
  • 问题内容: 我有一个自定义适配器,可显示订单列表中的每一行。 从Web服务查询新的订单列表之后,我想更新ListView的内容,因此我让Activity在调用notifyDataSetChanged()之前进行更新。 但是,从不调用OrderRowAdapter的getView()方法。ListView永远不会更新。 问题答案: 原来我不被叫的问题是因为它不可见。我的布局XML已经上与它的高度。因

  • 大家好,我在下面的链接中介绍了使用Python for Alexa的事实技能教程:https://github.com/alexa/skill-sample-python-fact 我的问题是Alexa只启动“启动请求” 但它不执行"GetNewFactIntent"的"IntentRequest" 正因为如此,只有调用名称在调用函数时有效,并且来自“GetNewFactIntent”的示例语句不

  • 问题内容: 我遵循以下示例:https : //code.google.com/p/mybatis/wiki/ResultHandlerExample 这是我的界面: 这是我的XML映射器 这段代码构成了我的DAO: 但是,永远不会调用结果处理程序。在这个例子中,我跟随人们说有同样的问题。那么如何使其工作呢?还是mybatis 3不支持结果处理程序? 问题答案: 我找到了答案。不幸的是,MyBat

  • 我创建了这个动画,通过停止动画进入 当SetAnimationDidStop的动画部分未被调用时。。。你能告诉我为什么,我哪里做错了吗? 这是一路动画:

  • 问题: 我正在尝试为Wordpress插件注册自定义endpoint。我面临的问题是,当我调用add_action('rest_api_init',回调)时,回调函数没有被调用。在该回调函数中存在“register_rest_route()”方法,而该方法又未被调用,我无法注册任何自定义终结点。 < li >我正在使用docker进行开发 < li >没有抛出任何错误 代码: 问题: 代码到达“v

  • 问题内容: 我有一个自定义我在我农具。 问题是该方法从未调用过。我想我需要在我的主适配器而不是自定义适配器中实现它。 MyListViewAdapter.java : 我对吗? 问题答案: 为什么在适配器内部会有OnItemClickListener?您可能在Adapter内有OnClickListener,或者最佳实践是将OnItemClickListener设置为ListView或您在活动/片