目前,我正在我的项目中处理推送通知。当推送通知显示时,我想在单击通知时打开我的通知活动,但它总是打开MainActivity为什么?我也用谷歌搜索了一下,但是找不到正确的答案。
这是我的MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MyNotification.class);
intent.putExtra("Message",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Shri Sidhbali Baba")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}下面是我的清单文件:
<activity android:name=".MyNotification"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name=".MyNotification" android:label="@string/app_name"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>如何在点击哦推送通知时打开我的通知活动。谢谢你的时间..。
发布于 2016-08-31 17:03:52
AFAIK,这段代码在应用程序处于前台时运行。当它在后台时,Firebase会将通知传送到系统托盘。我也遇到过类似的问题。当它传送到系统托盘时,它总是打开MainActivity。我注意到firebase允许将参数传递给Intent。我利用这种技术来读取自定义字符串(从firebase消息控制台打开高级选项,并将键指定为'item‘,将值指定为'MyNotification')。从主活动中读取此字符串,并将控件从MainActivity::onCreate()方法重定向到适当的活动
//private String ITEM = "item";
@Override
protected void onCreate(Bundle savedInstanceState) {
//Parse the input passed to the activity
Intent intent = getIntent();
String input = intent.getStringExtra(ITEM);
//Redirect to MyNotification
Intent redirect = new Intent(this, MyNotification.class);
startActivity(redirect);
}发布于 2016-08-31 14:59:28
更新此内容:
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MyNotification.class);
intent.putExtra("Message",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyNotification.class), 0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Shri Sidhbali Baba")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setContentIntent(contentIntent);
notificationManager.notify(0, notificationBuilder.build());
}发布于 2016-08-31 15:03:37
尝试在清单中删除此行
android:parentActivityName=".MainActivity"编辑1:你可以在这里阅读更多https://developer.android.com/training/implementing-navigation/ancestral.html
原因是当您调用"MyNotification“活动时,back堆栈将”自动“将其父活动添加到back堆栈中,因此您可以按back返回到您的"MainActivity”
https://stackoverflow.com/questions/39242612
复制相似问题