目前我正在我的项目中处理推送通知。当推送通知显示时,我想在单击通知时打开 Mynotification Activity ,但它总是打开 MainActivity 为什么?我也为此谷歌和 SO 但找不到正确的答案。
这是我的 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());
}
}
这是我的 list 文件:
<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>
单击哦推送通知时如何打开 Mynotification Activity 。 感谢您的宝贵时间...
最佳答案
据我所知,这段代码在应用程序位于前台时运行。当它在后台时,Firebase 将通知发送到系统托盘。我遇到过类似的问题。当它传送到系统托盘时,它总是打开 MainActivity。我注意到 firebase 允许将参数传递给 Intent。我利用这种技术来读取自定义字符串(从 firebase 消息控制台打开高级选项并将键指定为“item”,将值指定为“MyNotification”)。 从主 Activity 中读取此字符串并将控件重定向到 MainActivity::onCreate() 方法中的适当 Activity
//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);
}
关于android - 单击推送通知时如何打开 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39242612/