• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - Firebase中应用程序在后台时如何处理通知

[复制链接]
菜鸟教程小白 发表于 2022-8-1 01:21:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的 list :

<service android:name=".fcm.PshycoFirebaseMessagingServices">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<service android:name=".fcm.PshycoFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>
当应用程序在后台并且通知到达时,默认通知会出现并且不运行我的 onMessageReceived 代码.
这是我的onMessageReceived代码。如果我的应用程序在前台运行,而不是在后台运行,则会调用此方法。当应用程序也在后台时,如何运行此代码?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    data = remoteMessage.getData();
    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String imageUrl = (String) data.get("image");
    String action = (String) data.get("action");
    Log.i(TAG, "onMessageReceived: title : "+title);
    Log.i(TAG, "onMessageReceived: message : "+message);
    Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
    Log.i(TAG, "onMessageReceived: action : "+action);

    if (imageUrl == null) {
        sendNotification(title,message,action);
    } else {
        new BigPictureNotification(this,title,message,imageUrl,action);
    }
}
// [END receive_message]



Best Answer-推荐答案


1. 为什么会这样?

FCM(Firebase Cloud Messaging)中有两种类型的消息:

  • 显示消息 : 这些消息触发 onMessageReceived()仅当您的应用位于 时回调前景
  • 数据信息 : 这些消息触发 onMessageReceived()回调 甚至如果您的应用位于 前景/背景/杀死

  • NOTE: Firebase team have not developed a UI to send data-messages to your devices, yet. You should use your server for sending this type!




    2. 怎么做?

    为此,您必须执行 POST请求以下 URL:

    POST https://fcm.googleapis.com/fcm/send



    header
  • key : Content-Type , 值(value): application/json
  • key : Authorization , 值(value): key=<your-server-key>

  • 正文使用主题
    {
        "to": "/topics/my_topic",
        "data": {
            "my_custom_key": "my_custom_value",
            "my_custom_key2": true
         }
    }
    

    或者,如果您想将其发送到特定设备
    {
        "data": {
            "my_custom_key": "my_custom_value",
            "my_custom_key2": true
         },
        "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
    }
    

    NOTE: Be sure you're not adding JSON key notification
    NOTE: To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key



    3. 推送通知消息如何处理?

    这是您处理收到的消息的方式:
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) { 
         Map<String, String> data = remoteMessage.getData();
         String myCustomKey = data.get("my_custom_key");
    
         // Manage data
    }
    

    关于android - Firebase中应用程序在后台时如何处理通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711082/

    回复

    使用道具 举报

    懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关注0

    粉丝2

    帖子830918

    发布主题
    阅读排行 更多
    广告位

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap