Firebase(FCM) notifications not coming when android app removed from recent tray

后端 未结 3 1493
囚心锁ツ
囚心锁ツ 2021-02-09 02:12

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

$url = \'https://fcm.googleapis.com/fcm/send\';
        $fields = array(
              


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 02:46

    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.

提交回复
热议问题