目前我正在处理项目中的推送通知。当推送通知显示我想在点击通知时打开Mynotification活动但它总是打开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());
}
}
这是我的清单文件:
<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活动。 谢谢你的时间......
答案 0 :(得分:2)
AFAIK,这段代码在应用程序位于前台时运行。当它在后台时,Firebase会将通知发送到系统托盘。我遇到过类似的问题。当它传送到系统托盘时,它总是打开MainActivity。我注意到firebase允许将参数传递给Intent。我利用这种技术来读取自定义字符串(从firebase消息控制台打开预先选项,并将键指定为&#39; item&#39;以及值为&#39; MyNotification&#39;)。 从主活动中读取此字符串,并将控件重定向到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);
}
答案 1 :(得分:1)
更新:
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());
}
答案 2 :(得分:0)
尝试删除Manifest中的这一行
android:parentActivityName=".MainActivity"
编辑1:您可以在此处阅读更多内容https://developer.android.com/training/implementing-navigation/ancestral.html
原因是当你打电话给你的'#It; MyNotification&#34;活动,后台将自动&#34;将它的父活动添加到您的后台,这样您就可以按回去恢复您的&#34; MainActivity&#34;
答案 3 :(得分:0)
试试这个
public class VideoNotification extends AppCompatActivity {
public Bitmap image ,bmp;
public NotificationCompat.Builder nb;
final String url = "https://www.google.es/images/srpr/logo11w.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_notification);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, VideoActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
nb = new NotificationCompat.Builder(this);
nb.setSmallIcon(R.drawable.icon);
nb.setContentTitle("Image Notification");
nb.setContentText("Set Content text");
nb.setTicker("Set Ticker text");
// nb.setStyle(new NotificationCompat.BigTextStyle().bigText("Big View Styles"));
/* try {
URL url = new URL("https://www.gstatic.com/webp/gallery3/1.png");
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
} */
Glide.
with(this).
load(url).
asBitmap()
.centerCrop()
.into(new SimpleTarget<Bitmap>(200,200) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(resource);
bps.setSummaryText("Summary text appears on expanding the notification");
nb.setStyle(bps);
}
});
// Width and height
// NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
// bps.setSummaryText("Summary text appears on expanding the notification");
// nb.setStyle(bps);
/* NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
bps.setSummaryText("Summary text appears on expanding the notification");
nb.setStyle(bps); */
// Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.picture);
// NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
// s.setSummaryText("Summary text appears on expanding the notification");
// nb.setStyle(s);
TaskStackBuilder TSB = TaskStackBuilder.create(this);
TSB.addParentStack(VideoNotification.class);
// Adds the Intent that starts the Activity to the top of the stack
TSB.addNextIntent(intent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(true);
nb.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nb.setSound(alarmSound, AudioManager.STREAM_MUSIC);
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(11221, nb.build());
}
}