简体   繁体   English

Android中的Firebase推送通知不起作用

[英]Firebase push notification in android not working

I implemented push notification in Android using Firebase and I am receiving push notification using Firebase console. 我在Android中使用Firebase实现了推送通知,并且正在使用Firebase控制台接收推送通知。 But when I try to connect to my server by sending device token and server key, then I am not receiving notifications (from my server). 但是,当我尝试通过发送设备令牌和服务器密钥连接到服务器时,则没有收到(来自服务器的)通知。

Our server side team tested and it is working at their end, but my device is not receiving push notifications (device on which app is installed) and I am receiving push notification from Firebase. 我们的服务器端团队已经过测试,并且可以正常使用,但是我的设备未收到推送通知(安装了应用程序的设备),并且我从Firebase收到了推送通知。

How can I connect to the server? 如何连接到服务器? Is the problem in Android or with creating the google-services.json file? 在Android中还是在创建google-services.json文件时出现问题?

While Using Firebase push notification there are two cases to handle data payload( eg your Activity name etc.). 使用Firebase推送通知时,有两种情况来处理数据有效负载(例如,您的“活动名称”等)。

case 1: When your app is running in foreground(currently running on screen) you would get data payload from RemoteMessage object in onMessageReceived(RemoteMessage remoteMessage) method that you have overridden in your class that must extend FirebaseMessagingService class. 情况1:当您的应用程序在前台运行(当前在屏幕上运行)时,您将从类中重写的onMessageReceived(RemoteMessage remoteMessage)方法中的RemoteMessage object获取数据有效负载, RemoteMessage object方法必须扩展FirebaseMessagingService类。

For example : 例如 :

public class FirebaseMessagingServiceHelper extends FirebaseMessagingService {
    private final String TAG = "FirebaseMsgServiceHelper";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Map<String, String> dataPayload = null;
    dataPayload = remoteMessage.getData();
    }
}

case 2: When your app is running in background you would get data payload as extras in intent in your app's launcher Activity . 情况2:当您的应用程序在后台运行时,您将获得数据有效载荷作为应用程序启动器Activity附加目的。

For Example :- 例如 :-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //get notification data info
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
    //bundle must contain all info sent in "data" field of the notification
   }
}

But these two cases were not working properly when I got name of the Activity that is supposed to be open when user clicks on a notification on Notification Drawer in click_action and data payload in data . 但是,这两种情况都不能正常工作时,我得到了名Activity是应该当用户点击一个通知是开放的Notification Drawerclick_action在和数据有效载荷data

But when I putted down everything in data it worked fine for me in both the cases ie whether app is running in foreground or in background your app would receive data payload everytime in RemoteMessage object in onMessageReceived(RemoteMessage remoteMessage) method. 但是,当我的推杆下来的数据,它的工作对我罚款在两种情况下的一切ie应用是否在前台或后台您的应用程序运行将接收数据有效载荷每次在RemoteMessage objectonMessageReceived(RemoteMessage remoteMessage)方法。

Below is the link I link from where I got idea. 以下是我从哪里获得想法的链接。 Firebase FCM notifications click_action payload Firebase FCM通知click_action有效负载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM