简体   繁体   中英

Firebase Notifications not being received when app is in background even if data payload is passed

Now I know that if I send data messages, the notifications will come even if the app is in the background. But I am receiving no notifications whatsoever even after sending data messages.

I'm using Postman for testing purposes and here's my body of my Postman Request:

{
 "to" : (device token),

 "data" : {
     "body" : "great match!",
     "title" : "Portugal vs. Denmark",
     "content_available" : true,
     "priority" : "high",
     "sound": "default",
     "time_to_live":"2419200"
  } 
}

And this is my onMessageReceived() funtion:

public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String,String> data=remoteMessage.getData();
    String title=data.get("title");
    String body=data.get("body");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            sendPushNotification(title,body);

        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

And this is what my sendPushNotification() function looks like:

private void sendPushNotification(String title,String message) {

    try {

        String imageUrl="";
        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        if(imageUrl.equals("null")){
            mNotificationManager.showSmallNotification(title, message, intent);
            mNotificationManager.playNotificationSound();
        }else{
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            mNotificationManager.playNotificationSound();
        }

    } catch (Exception e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    }
}

I was following this question on stack overflow : How to handle notification when app in background in Firebase

PS: When app is in the foreground, notifications are being received just fine.

As you could see on my post, for specific ids you should use "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] key and values instead of ”to” . That's for topics

You need to define click_action in your notification payload.

"notification"{
     "click_action":"YOUR_ACTIVITY_NAME"
  }

also in your manifest edit the activity definition as below

<activity
            android:name="com.demo.xyzActivity"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name=".xyzActivity" /> //This should be one set into payload
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

This will open your activity when app will be in background.

are you using notification payload along with data payload? as is the function finds notification payload it sends it directly to notification tray and hence data payload is neglected. change

JSONObject json = new JSONObject(remoteMessage.getData().toString());

to

Map<String, String> bundleData = remoteMessage.getData();

this will remove try catch and will let you know in its causing exception.

Create the BroadcastReceiver

Declare in Manifest.xml

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <receiver
                android:name=".OnBootBroadcastReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
    </receiver>

Create OnBootBroadcastReceiver.java file and call the FCM service in it.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
        i.setClass(context, FirebaseMessagingReceiveService.class);
        context.startService(i);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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