这是我正在使用的代码。我可以听到通知有效,但没有文字。
@Override
public void onReceive(Context context, Intent arg1) {
showNotification(context);
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MyActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
答案 0 :(得分:3)
如果您使用的是support-v4库,请使用此代码
private void showNotification(Context context) {
PendingIntent pIntent = PendingIntent.getActivity(context, 0,
new Intent(context, DashBoard.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.getNotification());
}
答案 1 :(得分:1)
您需要添加图标 new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher);
答案 2 :(得分:0)
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));