阅读量:0
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服务器协议来发送通知。