问题
Sorry for bad english!!! I am developing my final year project where i need a chat box. i have successfully develop the chat system. but there is little bit of problem with notification. When my app is running on device it receives all the notifications but after killing or closing the application no notification arrived on y device. i have tested it on many devices but all in vain. Please help on this because i have to submit this on next monday.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getNotification()!=null){
String title=remoteMessage.getNotification().getTitle();
String body=remoteMessage.getNotification().getBody();
NotificationHelper.displayNotification(getApplicationContext(),title,body);
}
}
}
public class NotificationHelper { public static void displayNotification(Context context,String title,String body){
Intent intent=new Intent(context,ChatActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(
context,
100,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context, ChatActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1,mBuilder.build());
}
}
来源:https://stackoverflow.com/questions/60578857/firebase-push-notification-is-not-generated-when-app-is-killed-in-android