新聞中心
Android 推送通知

創(chuàng)新互聯(lián)專注于源城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供源城營銷型網(wǎng)站建設(shè),源城網(wǎng)站制作、源城網(wǎng)頁設(shè)計、源城網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造源城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供源城網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
概述
在Android應(yīng)用開發(fā)中,推送通知是與用戶進行交互的一種重要方式,它可以在用戶的設(shè)備上顯示消息、提醒和更新,即使應(yīng)用沒有運行也能收到這些信息,以下是關(guān)于如何實現(xiàn)Android推送通知的詳細步驟:
1. 集成Firebase Cloud Messaging (FCM)
步驟1: 添加依賴項
在你的build.gradle文件中,添加以下依賴項:
implementation 'com.google.firebase:firebasemessaging:20.1.0'
步驟2: 配置Firebase項目
在你的Firebase控制臺中,創(chuàng)建一個新項目或選擇一個現(xiàn)有項目,然后添加你的Android應(yīng)用,這將生成一個googleservices.json文件,你需要將其添加到你的應(yīng)用模塊的根目錄中。
步驟3: 初始化Firebase
在你的應(yīng)用的主活動中,初始化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. 創(chuàng)建通知通道
從Android O(8.0)開始,你需要為每個通知創(chuàng)建一個通知通道,以下是如何創(chuàng)建一個通知通道的示例:
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. 接收推送通知
要接收推送通知,你需要創(chuàng)建一個服務(wù)來處理這些通知,以下是一個簡單的服務(wù)示例:
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. 請求通知權(quán)限
如果你的應(yīng)用需要顯示通知,你需要在運行時請求通知權(quán)限,你可以在你的主活動中添加以下代碼來實現(xiàn)這一點:
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. 發(fā)送推送通知
你可以使用Firebase控制臺或你自己的服務(wù)器來發(fā)送推送通知,如果你選擇使用自己的服務(wù)器,你需要使用FCM服務(wù)器協(xié)議來發(fā)送通知。
網(wǎng)站名稱:android推送_Android
當(dāng)前URL:http://fisionsoft.com.cn/article/ccccsid.html


咨詢
建站咨詢
