单击通知后如何进行特定活动
Posted
技术标签:
【中文标题】单击通知后如何进行特定活动【英文标题】:how i can go on particular activity after clicking on notification 【发布时间】:2017-02-27 07:02:04 【问题描述】:我希望我的应用程序应该给我通知,并在点击通知时打开另一个活动,例如 whatsapp 通知 这是我的代码 c 有人帮忙吗???
public class MyFirebaseMessagingService extends FirebaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
// sendNotification(remoteMessage.getNotification().getBody());
//Bundle bundle=new Bundle();
//bundle.putString("msgBody",remoteMessage.getNotification().getBody());
//intent use for start this activity after click on notification
Intent intent = new Intent(getApplicationContext(),Secondactivity.class);
String valu=remoteMessage.getNotification().getBody();
intent.putExtra("notificationmessage",valu);
**strong text** //here we are telling system after clicking you have to come on mainactivity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//here we are giving rights to main activity.FLAG_ONE_SHOT useful to indicate this pending intent can use only once
PendingIntent pendingintent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
//notificationcompat useful for creating notification layout
NotificationCompat.Builder notificationbuilder=new NotificationCompat.Builder(this);
notificationbuilder.setContentText(remoteMessage.getNotification().getBody());
notificationbuilder.setContentTitle("FCM NOTIFICATION");
notificationbuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationbuilder.setAutoCancel(true);
notificationbuilder.setContentIntent(pendingintent);
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationbuilder.build());
【问题讨论】:
【参考方案1】:您必须在消息内容中添加一个额外的参数来识别类型。然后根据该类型值创建具有不同活动的通知管理器。
例如:
switch (type)
case 1:
notification manager for activity A
break:
case 2:
notification manager for activity B
break:
【讨论】:
以上是关于单击通知后如何进行特定活动的主要内容,如果未能解决你的问题,请参考以下文章
Android 通知单击保持堆栈并在需要时将应用程序置于最前面。如果特定活动在顶部,则传递数据