我需要在click事件中使用通知,我有通知方法,但此方法无法打开我的活动。
我的代码:
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
这可能吗,
谢谢。
是的,有可能。
像这样改变你的方法;
private void sendNotification(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(getNotificationIcon(), "Action Button", pIntent)
.setContentIntent(pIntent)
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
并添加您的主要活动
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
此代码有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句