Firebase 推送通知更新数据库 [英] Firebase push notifications update DB

查看:51
本文介绍了Firebase 推送通知更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过新的 Firebase 推送通知服务向我的用户发送一些数据.

我正在添加一些带有消息文本"-dbupdate"的键值"自定义数据项,以检测它是更新消息还是我想要向用户显示的普通推送通知.

在我的接收器中,我检查是否有自定义数据并运行更新(如果为真).如果不是这种情况,我会通过短信显示正常通知.

它仅在我的应用程序打开时有效.如果应用程序关闭,则无论如何都会显示通知,而我的 if 检查甚至没有运行,

一些代码:

public class PushMessageService 扩展 FirebaseMessagingService {@覆盖public void onMessageReceived(RemoteMessage remoteMessage) {super.onMessageReceived(remoteMessage);映射<字符串,字符串>数据 = remoteMessage.getData();String link = data.get("link");String name = data.get("name");如果(链接!=空){Log.d("链接",链接);Log.d("name",name);UpdateHelper.Go(this, link, name);}别的 {NotificationCompat.Builder mBuilder =新的 NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_shop_white_24dp).setAutoCancel(true).setContentTitle(remoteMessage.getNotification().getTitle()).setContentText(remoteMessage.getNotification().getBody());通知管理器 mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(5, mBuilder.build());}}}

示例通知

解决方案:

发送推送"消息有两种方式.

1.Firebase 控制台

2.消息传递 API - 显示使用高级 REST 客户端的请求的图片

onMessageReceived(...) 方法中接收推送消息.Firebase onMessageReceived(...) 方法不会被调用如果应用程序处于后台或被杀死,仅当该消息是通过 Firebase 控制台发送的.

如果您通过 API 发送消息,则可以正常工作.无论应用程序是在后台、前台还是被杀死,消息都在 onMessageReceived(...) 方法中传递.(注意:这在 >=Lollipop 中工作正常 - Android 4.4 或更低版本的行为仍然无法预测.

更新:

您可以像这样从 API(firebase 称其为下游 msg)发送推送消息:https://firebase.google.com/docs/cloud-messaging/downstream

从服务器发送下游消息

为了处理或定位"下游消息,应用服务器使用接收客户端应用的注册令牌设置为.您可以发送带有预定义字段的通知消息,或自定义数据消息;有关有效负载支持的详细信息,请参阅消息有效负载中的通知和数据.此页面中的示例展示了如何使用 HTTP 和 XMPP 协议发送数据消息.

HTTP POST 请求

https://fcm.googleapis.com/fcm/send内容类型:应用程序/json授权:key=AIzaSyZ-1u...0GBYzPu7Udno5aA{ 数据": {"score": "5x1",时间":15:10"},到":bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."}

I'm trying to send some data to my users via the new Firebase push notifications service.

I'm adding some "key-value" custom data items with 'Message text' - "dbupdate" to detect if it's an update message or a normal push notification that I want to show my users.

In my receiver I check i there is custom data and run the update, if true. If this isn't the case, I show a normal notification with the text message.

It works only when my application opened. If the application is closed, the notification is shown anyway, and my if check doesn't even running,

some code:

public class PushMessageService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map<String,String> data = remoteMessage.getData();
        String link = data.get("link");
        String name = data.get("name");
        if (link!=null) {
            Log.d("link",link);
            Log.d("name",name);
            UpdateHelper.Go(this, link, name);
        }
        else {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_shop_white_24dp)
                            .setAutoCancel(true)
                    .setContentTitle(remoteMessage.getNotification().getTitle())
                   .setContentText(remoteMessage.getNotification().getBody());
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(5, mBuilder.build());
        }
    }
}

example notficiation here

Why is that so? What can I do to run my code even when the application closed?

解决方案

As per my experience with Firebase push:

Problem: When sending push notification through "Firebase console", notification is only received when application is in foreground. If the application is killed or in background, the notification is shown only in Version >= lollipop with incorrect notification icon and/or opening the default launcher activity in the app even if you have defined different Activity in PendingIntent.

As per Firebase docs, a push message is divided into 2 pars, they call them: Docs Link here

  1. notification
  2. data payload

Solution:

There are two ways to send a "Push" message.

1. Firebase Console

2. Messaging API - Image showing a request using Advanced REST Client

Push message is received in onMessageReceived(...) method. Firebase onMessageReceived(...) method will not get called if the app is in background or killed only when the message is sent through Firebase Console.

If you send the message via API, it works fine. Message is delivered in onMessageReceived(...) method whether app is in background, foreground or killed. (Caution: This is working fine in >=Lollipop - Behaviour for Android 4.4 or below is still unpredictable.

Update:

You can send a push message from API (firebase call it downstream msg) like this: https://firebase.google.com/docs/cloud-messaging/downstream

Sending downstream messages from the server

To address or "target" a downstream message, the app server sets to with the receiving client app's registration token. You can send notification messages with predefined fields, or custom data messages; see Notifications and data in the message payload for details on payload support. Examples in this page show how to send data messages in HTTP and XMPP protocols.

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

这篇关于Firebase 推送通知更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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