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推送的通知了。请记得将代码中的图标和通知内容替换为你自己的内容。

相关内容

热门资讯

透视游戏!雀神辅助器免费版v2... 透视游戏!雀神辅助器免费版v2.0(透视)太坑了果真是真的有挂(有挂总结)-哔哩哔哩1、不需要AI权...
透视辅助!边锋老友棋牌免费挂,... 透视辅助!边锋老友棋牌免费挂,uupoker有挂,详细有挂(有挂攻略)-哔哩哔哩;小薇(透视辅助)致...
最新技巧!小程序边锋干瞪眼辅助... 最新技巧!小程序边锋干瞪眼辅助app(WEpoke),太坑了确实真的有挂(有挂教程)-哔哩哔哩;(需...
玩家必备教程(Wepoke大厅... 自定义Wepoke大厅房系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不...
一分钟带你了解!广东雀神麻将让... 1、一分钟带你了解!广东雀神麻将让系统发好牌(透视)太坑了原来真的有挂(有挂方法)-哔哩哔哩;该软件...
玩家必看科普!边锋干瞪眼辅助器... 玩家必看科普!边锋干瞪眼辅助器,扑克时间辅助用,详细有挂(有挂了解)-哔哩哔哩1、玩家可以在边锋干瞪...
透视系统!边锋老友棋牌能装挂(... 透视系统!边锋老友棋牌能装挂(wepokE),太坑了原来是真的有挂(有挂详情)-哔哩哔哩是一款可以让...
科技分享(线上德州)外挂透明挂... 科技分享(线上德州)外挂透明挂辅助软件(智能)发牌机制(2023已更新)(哔哩哔哩);玩家必备必赢加...
详细说明!边锋杭麻圈有挂,po... 详细说明!边锋杭麻圈有挂,pokerrrr2最新版本更新内容,详细有挂(有挂总结)-哔哩哔哩;一、边...
分享给玩家!雀神辅助器叫名字(... 分享给玩家!雀神辅助器叫名字(透视)太坑了的确是真的有挂(有挂教程)-哔哩哔哩;最新版2024是一款...