[英]click on notification not opening the activity
what is wrong with my code below? 我下面的代码有什么问题? I expect the MainActivity is opened up after notification is touched/clicked.
我希望在触摸/单击通知后打开MainActivity。 my code is below:
我的代码如下:
private void sendNotification(Quote quote) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
String message = quote.getQuote() + " - " + quote.getAuthor();
// Creates an Intent for the Activity
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra(Intent.EXTRA_SUBJECT, DailyQuotes.NOTIFICATION_QOD_MODE);
notifyIntent.putExtra(Intent.EXTRA_TEXT, quote.getQuoteID());
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.qod_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(DailyQuotes.NOTIFICATION_QOD_ID, mBuilder.build());
}
Please can anyone help me on this. 请任何人可以帮助我。
try this: 尝试这个:
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
int requestID = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(this,
MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(
this, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentText("This is test");
mBuilder.setContentIntent(contentIntent);
mBuilder.setContentTitle("Title").setSmallIcon(
R.drawable.ic_launcher);
mNotifyManager.notify(requestID, mBuilder.build());
you can change the flags. 您可以更改标志。
hope it helps. 希望能帮助到你。 i have changed the request id.
我已经更改了请求ID。
you should use unique id for each notification. 您应该为每个通知使用唯一的ID。 have a look over here.
在这里看看。 How to create Multiple statusbar Notifications in android
如何在Android中创建多个状态栏通知
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.