Skip to content Skip to sidebar Skip to footer

Why My Notification Not Work In Background When My App Is Kill And Close In Android

When my app is open, notification works perfectly but when in background it doesn't work .I want the notification to be displayed even when my app is killed or closed in my phone.

Solution 1:

Check your logcat if you are receiving the notification. IS your notification being displayed in the system tray?

Solution 2:

There are two types of Message types

  1. Notification message

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

  1. Data message

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

as per my comment

You need to send notification data in Data messages

SAMPLE FORMAT

{"message":{"token":"your token","data":{"key1":"value1","key2":"value2","key3":"value4",}}}

EDIT

NotificationManagernotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();

    Intentintent=newIntent(this, YourActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        intent.putExtra("ID",object.getString("data"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);


    NotificationManagernotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    StringNOTIFICATION_CHANNEL_ID="Your_channel";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannelnotificationChannel=newNotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH);

        notificationChannel.setDescription("Description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(newlong[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }


    NotificationCompat.BuildernotificationBuilder=newNotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true)
            .setColor(ContextCompat.getColor(this, R.color.colorDarkBlue))
            .setContentTitle(getString(R.string.app_name))
            .setContentText(remoteMessage.getNotification().getBody())
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX);

    notificationManager.notify(1000, notificationBuilder.build());
    notificationManager.cancelAll();

Post a Comment for "Why My Notification Not Work In Background When My App Is Kill And Close In Android"