gpt4 book ai didi

android - 使用 Android Studio 的 FCM 通知

转载 作者:行者123 更新时间:2023-11-29 17:12:56 25 4
gpt4 key购买 nike

我设法从 PHP 文件发送通知消息,并使用 Firebase 云消息传递 (FCM) 发送到手机。

一旦用户点击移动托盘上的通知消息,我如何重定向到具有相同消息以及时间和日期的另一个布局?

这是我在 FirebaseMessagingService.java 中的代码(使用 Android Studio)

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {

Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Notification")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent)
.setSound(defaultSoundUri);



NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manager.notify(0,builder.build());
}

最佳答案

检查这个。

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, 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("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, notificationBuilder.build());
}

关于android - 使用 Android Studio 的 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40021484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com