这是我点击按钮时调用的方法。
public void onDisplayNotification(View v)
{
Intent i = new Intent(this, Notification_Activity.class);
i.putExtra("Code", "Notification Dismissed");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setContentTitle("Meeting").setContentText("In 5 minutes").setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("my","Hello");
nm.notify(0, nb.build());
}
点击按钮时不显示任何内容。我检查了log cat,方法正在运行。
答案 0 :(得分:2)
必填通知内容
Notification对象必须包含以下内容:
A small icon, set by setSmallIcon() A title, set by setContentTitle() Detail text, set by setContentText()
尝试为通知构建器提供小图标资源。
答案 1 :(得分:1)
使用类似的东西:
public void createNotification(View view)
{
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
答案 2 :(得分:1)
int NOTIFICATION_ID = 1
NotificationManager mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Test";
CharSequence NotificationTitle = "Test";
// specify the notification icon and time
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, System.currentTimeMillis());
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, NotificationTitle,
NotificationContent, contentIntent);
notification.flags |= Notification.FLAG_SHOW_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION_ID, notification);
答案 3 :(得分:-1)
好nb.build()
类型方法对我不起作用。我上个月就这样试过,但效果很好:
NotificationCompat.Builder builder = (new NotificationCompat.Builder(this)
.setContentTitle("Hello Notification").setContentText("Notification Time!")
.setSmallIcon(R.drawable.ic_launcher).setContentIntent("your pending intent").setWhen(System.currentTimeMillis()));
Notification notif = builder.getNotification();
notif.defaults |= Notification.DEFAULT_SOUND;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify("put any id", notif);
stopSelf();