简体   繁体   中英

Not receiving notification when the app is killed

I have created a program that will add a notification in the android app. But somehow I'm not able to get notifications when application killed

Here is my FirebaseMessagingService

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getData() != null)
            sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage remoteMessage){

        Map<String, String> map = remoteMessage.getData();
        String title = map.get("title");
        String content = map.get("content");

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "MyAndroidChannel";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    "My Notification", NotificationManager.IMPORTANCE_MAX);

            notificationChannel.setDescription("This is a sample notification");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationChannel.enableVibration(true);

        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        builder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setTicker("Hearty365")
                .setContentTitle(title)
                .setContentText(content)
                .setContentInfo("info");

        notificationManager.notify(1,builder.build());


    }   
}

I've added a smallIcon, title and content. Also, I've defined a channel id as well.

But I don't know what is going on... Notifications are able to receive when the app is running and in background mode. But it's not receiving when the app killed.

Here is my FirebaseMessagingIdService class

public class FirebaseMessagingIdService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        sendNewTokenServer(FirebaseInstanceId.getInstance().getToken());
    }

    private void sendNewTokenServer(String token){
        Log.d("TOKEN",String.valueOf(token));
    }
}

I don't think I've done something wrong but please help me.

Thanks in advance.

From where you are trying to send message? If you are sending from your server, then try to have data payload in notification object. Then only app will receive message even when app is closed.

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