我发现我使用了一种弃用的方法(notification.setLatestEventInfo())
它说使用Notification.Builder。
当我尝试创建一个新实例时,它会告诉我:
Notification.Builder cannot be resolved to a type
答案 0 :(得分:151)
Notification.Builder API 11或NotificationCompat.Builder API 1
这是一个用法示例。
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
答案 1 :(得分:86)
This在API 11中,因此如果您正在为早于3.0的任何内容进行开发,则应继续使用旧API。
更新:NotificationCompat.Builder类已添加到支持包中,因此我们可以使用它来支持API级别v4及更高版本:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
答案 2 :(得分:70)
除了所选答案之外,还有来自Source Tricks的NotificationCompat.Builder
课程的示例代码:
// Add app running notification
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
}
// Remove notification
private void removeNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(FM_NOTIFICATION_ID);
}
答案 3 :(得分:4)
Notification Builder严格用于Android API Level 11及以上版本(Android 3.0及更高版本)。
因此,如果您不是针对Honeycomb平板电脑,则不应使用通知生成器,而应遵循以下example之类的旧通知创建方法。
答案 4 :(得分:2)
我在构建通知时遇到问题(仅适用于Android 4.0及更高版本)。 This link向我展示了我做错了什么并说出以下内容:
Required notification contents
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
基本上我错过了其中一个。作为对此进行故障排除的基础,请确保至少具备所有这些功能。希望这会让别人头疼。
答案 5 :(得分:2)
请访问Notifications Updates链接了解详情。
Android N还允许您将类似的通知捆绑显示为 一个通知。为了实现这一点,Android N使用了 现有的
NotificationCompat.Builder.setGroup()
方法。用户可以 展开每个通知,并执行回复等操作 每个通知单独从中删除 通知阴影。这是一个预先存在的示例,显示了一个发送的简单服务 使用NotificationCompat的通知。每个未读的对话来自 用户将作为不同的通知发送。
此示例已更新,以利用新通知 Android N中提供的功能。
答案 6 :(得分:1)
如果它可以帮助任何人......在针对较新的旧API进行测试时,使用支持包设置通知时遇到了很多麻烦。我能够让他们在更新的设备上工作,但会在旧设备上进行错误测试。 最终让我工作的是删除与通知功能相关的所有导入。特别是NotificationCompat和TaskStackBuilder。似乎在开始设置我的代码时,从较新的构建而不是从支持包添加的导入。然后当我想在eclipse中稍后实现这些项目时,我没有被提示再次导入它们。希望有意义,并帮助其他人:)
答案 7 :(得分:1)
即使在API 8中也能正常工作 你可以使用这段代码:
Notification n =
new Notification(R.drawable.yourownpicturehere, getString(R.string.noticeMe),
System.currentTimeMillis());
PendingIntent i=PendingIntent.getActivity(this, 0,
new Intent(this, NotifyActivity.class),
0);
n.setLatestEventInfo(getApplicationContext(), getString(R.string.title), getString(R.string.message), i);
n.number=++count;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.flags |= Notification.DEFAULT_SOUND;
n.flags |= Notification.DEFAULT_VIBRATE;
n.ledARGB = 0xff0000ff;
n.flags |= Notification.FLAG_SHOW_LIGHTS;
// Now invoke the Notification Service
String notifService = Context.NOTIFICATION_SERVICE;
NotificationManager mgr =
(NotificationManager) getSystemService(notifService);
mgr.notify(NOTIFICATION_ID, n);
或者我建议您关注这个
的优秀tutorial答案 8 :(得分:1)
我用过
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
答案 9 :(得分:0)
// This is a working Notification
private static final int NotificID=01;
b= (Button) findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification=new Notification.Builder(MainActivity.this)
.setContentTitle("Notification Title")
.setContentText("Notification Description")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NotificID,notification);
}
});
}
答案 10 :(得分:0)
自包含示例
与this answer中的技术相同,但是:
来源:
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button = new Button(this);
button.setText("click me");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Notification notification = new Notification.Builder(Main.this)
/* Make app open when you click on the notification. */
.setContentIntent(PendingIntent.getActivity(
Main.this,
Main.this.i,
new Intent(Main.this, Main.class),
PendingIntent.FLAG_CANCEL_CURRENT))
.setContentTitle("title")
.setAutoCancel(true)
.setContentText(String.format("id = %d", Main.this.i))
// Starting on Android 5, only the alpha channel of the image matters.
// https://stackoverflow.com/a/35278871/895245
// `android.R.drawable` resources all seem suitable.
.setSmallIcon(android.R.drawable.star_on)
// Color of the background on which the alpha image wil drawn white.
.setColor(Color.RED)
.build();
final NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Main.this.i, notification);
// If the same ID were used twice, the second notification would replace the first one.
//notificationManager.notify(0, notification);
Main.this.i++;
}
});
this.setContentView(button);
}
}
在Android 22中测试。