安卓系统是否会阻止 FCM 通知?
创始人
2024-11-09 15:30:43
0

安卓系统不会主动阻止 FCM(Firebase Cloud Messaging)通知,但是在某些情况下,可能会出现通知被系统阻止的情况。

以下是一种解决方法的代码示例,可以通过设置通知通道(Notification Channel)来提高通知的可靠性,并避免被系统阻止:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

public class NotificationHelper {

    private static final String CHANNEL_ID = "FCM_CHANNEL_ID";
    private static final String CHANNEL_NAME = "FCM_CHANNEL_NAME";

    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

在你的应用程序的入口点(例如 Application 类的 onCreate 方法)中,调用 createNotificationChannel 方法来创建通知通道:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationHelper.createNotificationChannel(this);
    }
}

这样,当你发送 FCM 通知时,可以使用指定的通道 ID,以确保通知可以正常显示而不被系统阻止:

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
    }

    private void showNotification(String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_notification);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

通过设置通知通道,可以确保 FCM 通知在安卓系统上的可靠性,并避免被系统阻止。

相关内容

热门资讯

一分钟私人局辅助器!德扑圈透视... 一分钟私人局辅助器!德扑圈透视挂,德扑圈有透视吗,详细教程(有挂插件)1、玩家可以在德扑圈透视挂软件...
三分钟俱乐部作弊!pokemm... 三分钟俱乐部作弊!pokemmo内置修改器(透视脚本辅助器)详细辅助外挂(果然外挂);1、下载好po...
六分钟软件下载!德普之星透视辅... 六分钟软件下载!德普之星透视辅助软件激活码(透视辅助)详细辅助外挂(原来外挂);1、每一步都需要思考...
9分钟科技!pokerworl... 9分钟科技!pokerworld辅助器(透视脚本)详细辅助助手(总是助手)1、该软件可以轻松地帮助玩...
二分钟透视免费!德扑圈透视挂(... 二分钟透视免费!德扑圈透视挂(透视辅助)详细辅助下载(一贯下载);1、德扑圈透视挂系统规律教程、德扑...
四分钟德州透视挂!智星菠萝有挂... 四分钟德州透视挂!智星菠萝有挂吗,pokemmo手机版脚本,详细教程(有挂内幕)1、任何pokemm...
八分钟安卓版!德普之星辅助器,... 八分钟安卓版!德普之星辅助器,德普之星透视辅助软件激活码,详细教程(有挂秘笈);1、玩家可以在德普之...
8分钟透视辅助软件!德普之星私... 8分钟透视辅助软件!德普之星私人局透视,德普之星怎么开辅助,详细教程(有挂插件)1、德普之星私人局透...
两分钟破解版!德州透视脚本,德... 两分钟破解版!德州透视脚本,德州局透视,详细教程(有挂秘笈);德州透视脚本辅助器中分为三种模型:德州...
十分钟有透视!德普之星的辅助工... 十分钟有透视!德普之星的辅助工具介绍,德普之星辅助软件,详细教程(有挂秘笈)1、不需要AI权限,帮助...