android推送_Android
创始人
2024-10-15 04:36:40
0

Android 推送通知

android推送_Android(图片来源网络,侵删)

概述

在Android应用开发中,推送通知是与用户进行交互的一种重要方式,它可以在用户的设备上显示消息、提醒和更新,即使应用没有运行也能收到这些信息,以下是关于如何实现Android推送通知的详细步骤:

1. 集成Firebase Cloud Messaging (FCM)

步骤1: 添加依赖项

在你的build.gradle文件中,添加以下依赖项:

 implementation 'com.google.firebase:firebasemessaging:20.1.0' 

步骤2: 配置Firebase项目

在你的Firebase控制台中,创建一个新项目或选择一个现有项目,然后添加你的Android应用,这将生成一个googleservices.json文件,你需要将其添加到你的应用模块的根目录中。

步骤3: 初始化Firebase

在你的应用的主活动中,初始化Firebase:

 import com.google.firebase.FirebaseApp; import com.google.firebase.messaging.FirebaseMessaging; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         FirebaseApp.initializeApp(this);         FirebaseMessaging.getInstance().setAutoInitEnabled(true);     } } 

2. 创建通知通道

从Android O(8.0)开始,你需要为每个通知创建一个通知通道,以下是如何创建一个通知通道的示例:

 private void createNotificationChannel() {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {         CharSequence name = "My Channel";         String description = "This is my channel";         int importance = NotificationManager.IMPORTANCE_DEFAULT;         NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance);         channel.setDescription(description);         // Register the channel with the system         NotificationManager notificationManager = getSystemService(NotificationManager.class);         notificationManager.createNotificationChannel(channel);     } } 

3. 接收推送通知

要接收推送通知,你需要创建一个服务来处理这些通知,以下是一个简单的服务示例:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {     @Override     public void onMessageReceived(RemoteMessage remoteMessage) {         super.onMessageReceived(remoteMessage);         // Check if message contains a notification payload.         if (remoteMessage.getNotification() != null) {             Log.d("FCM", "Message Notification Body: " + remoteMessage.getNotification().getBody());             sendNotification(remoteMessage.getNotification().getBody());         }     }     private void sendNotification(String messageBody) {         Intent intent = new Intent(this, MainActivity.class);         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,                 PendingIntent.FLAG_ONE_SHOT);         Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)                 .setSmallIcon(R.mipmap.ic_launcher)                 .setContentTitle("FCM Message")                 .setContentText(messageBody)                 .setAutoCancel(true)                 .setSound(defaultSoundUri)                 .setContentIntent(pendingIntent);         NotificationManager notificationManager =                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);         // Since android Oreo notification channel is needed.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {             NotificationChannel channel = new NotificationChannel("CHANNEL_ID",                     "Channel human readable name",                     NotificationManager.IMPORTANCE_DEFAULT);             notificationManager.createNotificationChannel(channel);         }         notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());     } } 

4. 请求通知权限

如果你的应用需要显示通知,你需要在运行时请求通知权限,你可以在你的主活动中添加以下代码来实现这一点:

 private void requestNotificationPermission() {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {         if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {             requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_CODE_PUSH_NOTIFICATIONS);         } else {             Log.d("FCM", "Notification permission granted");         }     } } 

5. 发送推送通知

你可以使用Firebase控制台或你自己的服务器来发送推送通知,如果你选择使用自己的服务器,你需要使用FCM服务器协议来发送通知。

相关内容

热门资讯

2026版方法!火神微信辅助(... 2026版方法!火神微信辅助(辅助)其实是真的有辅助神器(有挂规律)1、点击下载安装,火神微信辅助脚...
操作辅助!奇迹陕西靖边锅子有没... 操作辅助!奇迹陕西靖边锅子有没有技巧!曝光真的有辅助挂(有挂教学)进入游戏-大厅左侧-新手福利-激活...
黑科技辅助挂!威信茶馆跑辅助器... 黑科技辅助挂!威信茶馆跑辅助器(辅助)果然确实有辅助app(真实有挂)1、在威信茶馆跑辅助器插件功能...
要领辅助!好友赣南插件!了解是... 要领辅助!好友赣南插件!了解是有辅助器(证实有挂)1)好友赣南插件免费钻石:进一步探索好友赣南插件免...
随着!决战平安京辅助软件(辅助... 随着!决战平安京辅助软件(辅助)切实存在有辅助挂(有挂透视)1、打开软件启动之后找到中间准星的标志长...
手册辅助!哈灵脚本修改器!解迷... 手册辅助!哈灵脚本修改器!解迷存在有辅助工具(有挂方法)运哈灵脚本修改器辅助工具,进入游戏界面。进入...
此事迅速冲上热搜!微信小程序财... 此事迅速冲上热搜!微信小程序财神十三张辅助(辅助)切实真的是有辅助挂(有挂教程)1、下载好微信小程序...
指引辅助!快玩炸翻天辅助工具!... 指引辅助!快玩炸翻天辅助工具!辅助是真的有辅助软件(真的有挂)在进入快玩炸翻天辅助工具软件靠谱后,参...
方法辅助挂!雀神广东麻将推倒胡... 方法辅助挂!雀神广东麻将推倒胡辅助器下载(辅助)真是真的是有辅助app(果真有挂)1、该软件可以轻松...
方案辅助!广西友玩修改器!普及... 方案辅助!广西友玩修改器!普及真的是有辅助技巧(真的有挂)广西友玩修改器透视方法中分为三种模型:广西...