Android 单击pushnotification时如何打开活动?
目前我正在我的项目中处理推送通知。当推送通知显示我想在单击通知时打开Mynotification活动,但它总是打开MainActivity为什么?我也用谷歌搜索,但找不到正确的答案 这是我的MyFirebaseMessagingService:Android 单击pushnotification时如何打开活动?,android,android-studio,push-notification,android-manifest,Android,Android Studio,Push Notification,Android Manifest,目前我正在我的项目中处理推送通知。当推送通知显示我想在单击通知时打开Mynotification活动,但它总是打开MainActivity为什么?我也用谷歌搜索,但找不到正确的答案 这是我的MyFirebaseMessagingService: public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMs
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>
单击“推送通知”时如何打开我的通知活动。
感谢您抽出时间…更新此信息:
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());
}
尝试删除清单中的此行
android:parentActivityName=".MainActivity"
编辑1:您可以在此处阅读更多内容
原因是当您调用“MyNotification”活动时,back stack会“自动”将其父活动添加到back stack中,这样您就可以按back返回到“MainActivity”了。好的,这段代码在应用程序位于前台时运行。当它在后台时,Firebase会将通知发送到系统托盘。我曾面临过类似的问题。当它传送到系统托盘时,它总是打开MainActivity。我注意到firebase允许将参数传递给意图。我利用这种技术来读取自定义字符串(从firebase消息控制台打开advance选项,并将键指定为'item',将值指定为'MyNotification')。 从主活动中读取此字符串,并从MainActivity::onCreate()方法将控件重定向到相应的活动 试试这个
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());
}
}
公共类VideoNotification扩展了AppCompative活动{
公共位图图像;
公共通知建筑商;
最终字符串url=”https://www.google.es/images/srpr/logo11w.png";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u image\u通知);
}
公共无效createNotification(视图){
//准备意图,如果
//已选择通知
意向意向=新意向(此为VideoActivity.class);
PendingEvent pIntent=PendingEvent.getActivity(this,(int)System.currentTimeMillis(),intent,0);
nb=新的通知兼容性生成器(本);
注:设置小图标(R.可绘制图标);
注:setContentTitle(“图像通知”);
注:设置内容文本(“设置内容文本”);
注:setTicker(“setTicker文本”);
//nb.setStyle(新的NotificationCompat.BigTextStyle().bigText(“大视图样式”));
/*试一试{
URL=新URL(“https://www.gstatic.com/webp/gallery3/1.png");
image=BitmapFactory.decodeStream(url.openConnection().getInputStream());
}捕获(IOE异常){
e、 printStackTrace();
} */
滑翔。
用(这个)。
加载(url)。
asBitmap()
.centerCrop()
.into(新SimpleTarget(200200){
@凌驾
ResourceReady(位图资源,GlideAnimation)上的公共无效@MinWan有何原因?添加此行notificationBuilder.setContentIntent(contentIntent);setContentIntent(pendingIntent);更改为setContentIntent(contentIntent);嘿,我在此处添加此行notificationBuilder.setContentIntent(contentIntent);让我们来看看。在我的FCM控制台中,高级选项用于删除应用程序…你能告诉我如何添加密钥吗?我指的是控制台中的“新消息”。firebase.google.com->通知单击“高级选项”,你会看到下面的转换事件。这是用于向你的应用程序发送通知的同一个控制台不适用于我。它显示错误无法启动t activity.intent为null。实际上我不想使用外部libs…但感谢您花时间@vishnumm93
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());
}
}