Android Oreo中的FCM推送通知问题
创始人
2024-08-14 22:00:55
0

在Android Oreo中,FCM(Firebase Cloud Messaging)推送通知需要做一些额外的适配工作。以下是一个解决Android Oreo中FCM推送通知问题的示例代码:

  1. 在AndroidManifest.xml文件中添加渠道声明:

  
    
  

  1. 在res/values/strings.xml文件中定义默认通知渠道ID:

    default_channel

  1. 创建一个NotificationHelper类来处理通知相关操作:
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 = "default_channel";
    private static final String CHANNEL_NAME = "Default Channel";
    private static final String CHANNEL_DESCRIPTION = "Default notification channel";

    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(CHANNEL_DESCRIPTION);
            notificationManager.createNotificationChannel(channel);
        }
    }
}
  1. 在FirebaseMessagingService中调用NotificationHelper类的方法:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 处理收到的消息
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    private void showNotification(String title, String body) {
        NotificationHelper.createNotificationChannel(this);

        // 创建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }
}

这样,在Android Oreo中,你就能够正确地显示FCM推送的通知了。请记得将代码中的图标和通知内容替换为你自己的内容。

相关内容

热门资讯

wpk德州ai辅助神器!wpk... wpk德州ai辅助神器!wpk有透视辅助吗,(wPk)本来有挂,软件教程(黑科技神器)1、在wpk德...
wpk德州透视外挂!wpk德州... wpk德州透视外挂!wpk德州透视外挂,(wpK)都是真的是有挂,AA德州教程(黑科技科技)该软件可...
AApoker透视辅助器安装教... AApoker透视辅助器安装教程!aapoker辅助器怎么用,(AApoker)总是存在有挂,专业教...
Wepoke黑科技设备!wep... Wepoke黑科技设备!wepoke透明挂哪里有,(WepOke)好像是有挂,我来教教你(黑科技辅助...
德扑ai助手!德扑ai智能机器... 德扑ai助手!德扑ai智能机器人代理,(德扑)竟然是真的有挂,详细教程(黑科技黑科技);一、德扑ai...
德扑之星ai代打风险分析!德扑... 德扑之星ai代打风险分析!德扑之星ai代打,(德扑之星)果然是真的有挂,力荐教程(黑科技插件)1)德...
德州ai购买!线上德州ai辅助... 德州ai购买!线上德州ai辅助,(德州ai)其实是有挂,揭秘教程(黑科技透视)1、很好的工具软件,可...
wepoke有没有挂!wepo... wepoke有没有挂!wepoke到底有没有挂,(we-poker)竟然真的有挂,科技教程(黑科技工...
AApoker辅助工具!aap... AApoker辅助工具!aapoker辅助实际测试,(AAPoker)原来真的有挂,力荐教程(黑科技...
微扑克ai软件!微扑克助手,(... 微扑克ai软件!微扑克助手,(微扑克)确实真的有挂,2025新版教程(黑科技透明挂)1、许多玩家不知...