使用 FCM 杀死应用程序时,Android 无法接收通知

时间:2021-03-08 11:26:30

标签: java android push-notification firebase-cloud-messaging

<块引用>

当应用程序在前台和后台时,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。我在这个类中使用 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());
}

}

1 个答案:

答案 0 :(得分:0)

当应用程序因一个原因被杀死时,您无法收到通知:

一些制造商,如华为、OPPO ecc...在应用未运行时无法推送通知,这是为了节省电池寿命。当然,重要的应用程序(WhatsApp、Instagram ecc...)默认例外。另一方面,有一种方法可以修复它,每个应用程序都可以通过设置->电池手动获取此权限(它会通过手机更换手机)。

现在您必须找到此权限,该权限使应用无需运行即可推送通知。提示:当我勾选这个时,它不起作用;我不得不通过设置->应用程序卸载应用程序,然后重新安装