NotificationManager notificationManager = (NotificationManager) CONTEXT
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "New Messege", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Opening application on notification select
Intent intent = new Intent(CONTEXT, Welcome.class);
//intent.putExtra("payload", payload);
PendingIntent pendingIntent = PendingIntent.getActivity(CONTEXT, 0,intent, 0);
notification.setLatestEventInfo(CONTEXT, "Message","message", pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
我的问题是,当我点击通知中心的通知时,它无法启动我的应用。基本上,点击我的通知后没有任何反应!我已经尝试了很多解决方案但没有任何反应。
我在应用程序运行时遇到另一个问题(不在后台)它还会在通知栏中显示通知。请指导我。 提前完成。
答案 0 :(得分:0)
你有没有尝试过:
notification.setContentIntent(pendingIntent);
看看这个精彩的教程,在通知上解释了很多这个: http://www.vogella.com/tutorials/AndroidNotifications/article.html
答案 1 :(得分:0)
希望此代码snippnet可以帮助您:
@SuppressLint("NewApi")
public void shownotification(Context context)
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
// .setDeleteIntent(getDeleteIntent())
// .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0))
.setOngoing(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
解释:此处中间是我的应用的通知ID eg:final int mId = 0;
,MYDEMOACTIVITY.class是我要点击通知后要启动的另一项活动。