我是android新手。我的应用程序中有很多活动,根据收到的通知,我必须打开特定的活动。 例如:如果我收到聊天,它应该打开聊天活动,如果我收到请求它应该打开请求页面等等。 如何实现?我已经搜索但没有找到任何解决方案。
enter code here
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, main.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
msg.append(newMessage + "\n");
}
};
答案 0 :(得分:0)
Intent notificationIntent = new Intent(context, main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
您正在创建并显示具有意图的通知(Intent notificationIntent = new Intent(context, main.class)
)。根据您想要的行为更改意图!
答案 1 :(得分:0)
您可以检查邮件内容,并根据设置您打开活动的首选项
if(msg_content==something)
{
Intent notificationIntent = new Intent(context, main.class);
}
else if()
{.....
.....}
else
{...}
将main.class更改为您想要的活动