Firebase push notification is not generated when app is killed in android?

天涯浪子 提交于 2020-04-18 12:33:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!