簡體   English   中英

使用 FCM 殺死應用程序時,Android 無法接收通知

[英]Android unable to receive notification when app is killed using FCM

Android 能夠在應用程序處於前台和后台時接收通知,但是當我殺死它導致無法接收通知時。

我不確定哪一部分出了問題。

AndroidManifest.xml 中的代碼

 <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

MyFirebaseMessagingService.java

我沒有為刷新令牌創建 MyFirebaseIdService.java 。 我在這個 class 中使用 onNewToken(String mToken) 代替。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onNewToken(String mToken) {
    super.onNewToken(mToken);
    Log.e("NEW_TOKEN",mToken);
}

這是我的 onMessageReceived 代碼。

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

    Log.d(TAG, "From: " + remoteMessage.getFrom());


    RemoteMessage.Notification notification = remoteMessage.getNotification();
    Map<String, String> data = remoteMessage.getData();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        sendNotification(notification, data);
    }

}

sendNotification 代碼,我嘗試讓它同時接收數據有效負載和通知有效負載,但它仍然無法正常工作。

@RequiresApi(api = Build.VERSION_CODES.O)
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {

    String title;
    String body;

    if (data != null) {
        title = data.get("title");
        body = data.get("body");
    } else {
        title = notification.getTitle();
        body = notification.getBody();
    }

    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)
            .setLargeIcon(icon)
            .setColor(Color.BLUE)
            .setSmallIcon(R.mipmap.ic_launcher);

    try {
        String picture_url = data.get("picture_url");
        if (picture_url != null && !"".equals(picture_url)) {
            URL url = new URL(picture_url);
            Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            notificationBuilder.setStyle(
                    new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
            );
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    notificationBuilder.setLights(Color.RED, 1000, 300);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);

創建頻道代碼

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                "channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT
        );
        channel.setDescription("channel description");
        channel.setShowBadge(true);
        channel.canShowBadge();
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        managerCompat.createNotificationChannel(channel);
    }

    managerCompat.notify(101,notificationBuilder.build());
}

}

由於一個原因殺死應用程序時您無法收到通知:

一些制造商,如 HUAWEI、OPPO ecc... 在應用程序未運行時無法推送通知,這是為了節省您的電池壽命。 當然,重要的應用程序(WhatsApp、Instagram ecc...)默認情況下會例外。 另一方面,有一種方法可以修復它,每個應用程序都可以通過設置->電池手動獲取此權限(它通過電話更改電話)。

現在你必須找到這個權限,它允許應用程序在不運行的情況下推送通知。 提示:當我勾選這個時,它不起作用; 我不得不通過設置->應用程序卸載應用程序,然后重新安裝它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM