Reputation: 3257
I've just migrated to androidx to implement firebase messaging.
I've changed everything to androidx and connect my app to firebase.
At first when I send a notification from firebase console it crashes.
To avoid the crash I put the FirebaseApp.initializeApp(this);
in my MainActivity.
But still I don't see any notification in the notification list.
In my Manifest
I put
<service android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
For MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "FirebaseSerive";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
Notification notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ss_icon)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(/*notification id*/0, notification);
}
}
After updating my code my emulator is displaying Failed to post notification on channel “null”
Thank you for the help in advance.Hope you can help me.
Upvotes: 0
Views: 271
Reputation: 11018
You can try using this channel thing introduced in OREO. May be that is causing the issue.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "FirebaseSerive";
public static final String NOTIFICATION_CHANNEL_ID = "IMP_NOTIFICATIONS";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Notification.Builder notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ss_icon);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
notification.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
mNotificationManager.notify(/*notification id*/0, notification.build());
}
}
Edit: may be you are getting notification payload instead of data, which you can check by using below code.
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Upvotes: 1