我需要创建一个简单的通知,将在通知栏中显示的声音和图标,如果可能的话?我还需要它与Android2.2兼容,所以我发现NotificationCompat.Builder可以与4以上的所有API兼容。如果有更好的解决方案,请随时提出来。
发布于 2014-12-31 21:30:04
工作示例:
Intent intent = new Intent(ctx, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
发布于 2019-01-31 02:13:06
在android 8.0中显示通知
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void show_Notification(){
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
String CHANNEL_ID="MYCHANNEL";
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
.setContentText("Heading")
.setContentTitle("subheading")
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)
.setChannelId(CHANNEL_ID)
.setSmallIcon(android.R.drawable.sym_action_chat)
.build();
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,notification);
}
发布于 2017-02-09 04:33:24
我做了这个方法,并且工作得很好。(在android 6.0.1中测试)
public void notifyThis(String title, String message) {
NotificationCompat.Builder b = new NotificationCompat.Builder(this.context);
b.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.favicon32)
.setTicker("{your tiny message}")
.setContentTitle(title)
.setContentText(message)
.setContentInfo("INFO");
NotificationManager nm = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, b.build());
}
https://stackoverflow.com/questions/13902115
复制相似问题