簡體   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