作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在通知点击事件上打开详细信息 Activity 。这是 PendingIntent
和 Notification
设置类。我添加了 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/
我是一名优秀的程序员,十分优秀!