繁体   English   中英

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

[英]Not receiving notification when the app is killed

我创建了一个程序,它将在 android 应用程序中添加通知。 但不知何故,当应用程序被杀死时我无法收到通知

这是我的 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());


    }   
}

我添加了一个小图标、标题和内容。 此外,我还定义了一个频道 ID。

但我不知道发生了什么......当应用程序运行并处于后台模式时,可以接收通知。 但是当应用程序终止时它没有收到。

这是我的 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));
    }
}

我不认为我做错了什么,但请帮助我。

提前致谢。

您尝试从哪里发送消息? 如果您从服务器发送,请尝试在通知 object 中包含数据有效负载。 然后即使应用程序关闭,也只有应用程序会收到消息。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM