简体   繁体   English

应用程序被杀死时没有收到通知

[英]Not receiving notification when the app is killed

I have created a program that will add a notification in the android app.我创建了一个程序,它将在 android 应用程序中添加通知。 But somehow I'm not able to get notifications when application killed但不知何故,当应用程序被杀死时我无法收到通知

Here is my FirebaseMessagingService这是我的 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.此外,我还定义了一个频道 ID。

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这是我的 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.如果您从服务器发送,请尝试在通知 object 中包含数据有效负载。 Then only app will receive message even when app is closed.然后即使应用程序关闭,也只有应用程序会收到消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android设置应用程序图标时的应用程序图标上的徽章编号或在接收Facebook应用程序等通知时被杀死 - On Android setting badge number on app icon when app is in background or killed when receiving notification like facebook app 应用程序被杀死时如何停止接收通知? - How to stop receiving notifications when app is killed? 即使应用程序被杀死也推送通知 - Push Notification even when app is killed 应用程序关闭或被杀死时未收到 Android FCM 通知 - Android FCM notification is not received when app is close or Killed 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM 当应用程序在后台或被杀死时,如何在收到通知时启动我的应用程序或活动? - how can i launch my app or an activity on notification received when app is in background or killed? 应用程序被杀死时后台TimerTask被杀死 - Background TimerTask gets killed when app is killed 即使应用被杀死,如何发送通知 - How to send notification even if app is killed 当应用程序被系统杀死时,如何删除通知? - How to remove notification when application is killed by the system? 服务被杀死时销毁前台通知 - Destroy foreground notification when the service get killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM