当Android应用从最近的托盘中移除时,Firebase(FCM)通知不会发送 [英] Firebase(FCM) notifications not coming when android app removed from recent tray

查看:224
本文介绍了当Android应用从最近的托盘中移除时,Firebase(FCM)通知不会发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过我自己的webservice发送Firebase通知,如下所示。

  $ url ='https:// fcm.googleapis.com/fcm/send; 
$ fields = array(
'registration_ids'=> $ tokens,
'data'=> $ message
);
$ headers = array(
'Authorization:key = SERVER_KEY',
'Content-Type:application / json'
);

1. App在前台和后台时没有任何问题,从最近托盘然后通知不来,我必须处理这个任何解决方案呢? (像Whatsup通知显示所有情况下,即使我强制停止从应用程序设置)

一旦收到通知,从onMessageReceived如何通过这些消息,身体内容到我的活动/碎片?



3.在某些情况下,我想发送通知给所有,而不是将所有的令牌添加到数组。任何其他方式来处理像发送到所有类似的东西?

  public class MyFirebaseMessagingService扩展FirebaseMessagingService {
私有static final String TAG =MsgService;
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.d(TAG,From:+ remoteMessage.getFrom());
Log.d(TAG,Data Payload:+ remoteMessage.getData());
sendNotification(remoteMessage.getData()。get(message));
}
private void sendNotification(String messageBody){
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Firebase推送通知)
.setContentText(messageBody )
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());





$ b

失败的设备:(通知不是




  • MI Redmi Note 2(5.0.2 Lollipop)

  • 华为荣誉5c(5.1.1棒棒糖)



成功设备:(通知从最近的托盘中移除)三星Galaxy Grand Prime(4.4 Kitkat) >

解决方案


1.当前台和后台没有通知任何问题,但如果我从最近的托盘中删除应用程序然后
通知不来,我必须处理这个任何解决方案为
这个? (如Whatsup通知显示所有情况下,即使I
强制从设置停止此应用程序)

根据您的代码,您必须在 remoteMessage.getNotification()。getBody()上得到空指针异常,因为您没有发送通知,而是从curl请求发送数据有效载荷。根据我个人的经验,你刚刚提到的问题尤其在Android Kitkat或以下的设备中看到。在棒棒糖或以上的Firebase推送似乎工作正常。


2.一旦我收到通知,从onMessageReceived如何传递这些消息,你可以从 onMessageReceived()发出一个广播 $ c>并在您的活动中注册一个Braodcast接收器,并以您的广播意图发送数据。示例代码:

Activity



  private BroadcastReceiver receiver; 

@Overrride
public void onCreate(Bundle savedInstanceState){

// your oncreate code

IntentFilter filter = new IntentFilter() ;
filter.addAction(SOME_ACTION);

receiver = new BroadcastReceiver(){
@Override $ b $ public void onReceive(Context context,Intent intent){
String message = intent.getStringExtra(message );
}
};
registerReceiver(receiver,filter);

$ b @Override
protected void onDestroy(){
if(receiver!= null){
unregisterReceiver(receiver);
receiver = null
}
super.onDestroy();



FirebaseMessagingService



  public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG =MsgService;
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.d(TAG,From:+ remoteMessage.getFrom());
Log.d(TAG,Notification Message Body:+ remoteMessage.getData());

Intent intent = new Intent();
intent.setAction(SOME_ACTION);
intent.putExtra(message,remoteMessage.getNotification()。getBody());
sendBroadcast(intent);




$ block $ $ $ b $ 3.在一些我想发送通知给所有的情况下,而不是将所有的令牌添加到数组。任何其他方式来处理像发送到所有
类似的东西?


没有什么像发送到所有在Firebase API中,唯一的方法就是使用Firebase控制台,它有自己的问题来处理白色图标问题,并且撕掉通知的自定义参数。


I'm sending Firebase notifications through my own webservice with PHP like below.

$url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );
        $headers = array(
            'Authorization:key = SERVER_KEY ',
            'Content-Type: application/json'
            );

1.Notifications are coming when App is in Foreground and Background without any issues , But if I removed app from recent tray then notifications not coming , I must have to handle this any solution for this ? (like Whatsup notifications are showing all scenarios even I force stopped this app from settings)

2.Once I received notification , from "onMessageReceived" how to pass these message,body content to my Activity /Fragments?

3.In some cases I want to send notification to all , Instead of adding all tokens to array . any other way to handle like "send to ALL" something like that ?

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Data Payload : " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("message"));
    }
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
} 

Failed devices: (Notifications are not coming after removed from Recent Tray)

  • MI Redmi Note 2 (5.0.2 Lollipop)
  • Huawei honor 5c (5.1.1 Lollipop)

Succeed devices: (Notifications are coming after removed from Recent Tray)

  • HTC One X (4.2 Jelly Bean)
  • Samsung galaxy grand prime (4.4 Kitkat)

解决方案

1.Notifications are coming when App is in Foreground and Background without any issues , But if I removed app from recent tray then notifications not coming , I must have to handle this any solution for this ? (like Whatsup notifications are showing all scenarios even I force stopped this app from settings)

As per your code you must be getting a null pointer exception on remoteMessage.getNotification().getBody() as you are not sending notification, instead you are sending data payload from your curl request. As per my personal experience, the issue you just mentioned is seen especially in Android Kitkat or below devices. On Lollipop or above Firebase push seems to work fine.

2.Once I received notification , from "onMessageReceived" how to pass these message,body content to my Activity /Fragments?

You can issue a Broadcast from onMessageReceived() and register a Braodcast Receiver in your Activity and send the data in your broadcast intent. Example code:

Activity

private BroadcastReceiver receiver;

@Overrride
public void onCreate(Bundle savedInstanceState){

  // your oncreate code

  IntentFilter filter = new IntentFilter();
  filter.addAction("SOME_ACTION");

  receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String message = intent.getStringExtra("message");
    }
  };
     registerReceiver(receiver, filter);
}

 @Override
 protected void onDestroy() {
  if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null
  }
  super.onDestroy();
 }

FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());

        Intent intent=new Intent();
        intent.setAction("SOME_ACTION");
        intent.putExtra("message", remoteMessage.getNotification().getBody());
        sendBroadcast(intent);
    }
} 

3.In some cases I want to send notification to all , Instead of adding all tokens to array . any other way to handle like "send to ALL" something like that ?

There is nothing like "Send to ALL" in Firebase API, the only way to do this is using Firebase Console which have its own issues to handle like white icon issue, and ripping the custom params of your notification.

这篇关于当Android应用从最近的托盘中移除时,Firebase(FCM)通知不会发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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