gpt4 book ai didi

android - PendingIntent 不适用于 FCM 通知

转载 作者:行者123 更新时间:2023-11-29 19:42:54 26 4
gpt4 key购买 nike

我想在通知点击事件上打开详细信息 Activity 。这是 PendingIntentNotification 设置类。我添加了 addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 还添加了 PendingIntent.FLAG_UPDATE_CURRENT。我在网站上搜索了所有主题,我找到了这个解决方案,我尝试了所有这些解决方案。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

ArrayList<String> notificationData = new ArrayList<String >(remoteMessage.getData().values());
String fortuneID = notificationData.get(0);
sendNotification(remoteMessage.getNotification().getBody(),fortuneID);
}
// [END receive_message]

/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody,String notificationID) {

Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
configureIntent.putExtra("extra", "123123");
configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
configureIntent.setAction("dummy_unique_action_identifyer" + "123123");
int dummyuniqueInt = new Random().nextInt(543254);
PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(getApplicationContext(), dummyuniqueInt, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

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

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("myApp")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingClearScreenIntent);

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

notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());
}
}

这是 getBundle 部分 MainActivity

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String quote = getIntent().getStringExtra("extra");

...
}

另一个问题我想打开 FalDetailActivity 但总是打开 MainActiviy 并且 getIntent() 总是返回 null。可能是我的 PendingIntent 未设置,我没有发现错误,请检查此代码。谢谢...

更新

我将 getBroadcast 更新为 getActivity 并删除了一些内容

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
configureIntent.putExtra("extra", "123123");
configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingClearScreenIntent = PendingIntent.getActivity(getApplicationContext(), 0, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

最佳答案

最后我找到了解决方案: https://firebase.google.com/docs/notifications/android/console-audience

当您的应用程序在后台运行时,Android 会将通知消息定向到系统托盘。默认情况下,用户点击通知会打开应用启动器。

这包括包含通知和数据负载的消息。在这些情况下,通知会传送到设备的系统托盘,并且数据负载会在启动器 Activity 的 intent 的额外部分中传送。

关于android - PendingIntent 不适用于 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38315397/

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