notification简单使用
1.不推荐 , 已不使用
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis()); Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //被删除 notification.setLatestEventInfo(this, "This is content title","This is content text", pi); manager.notify(1, notification);
2.系统推荐
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, DD.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(android.R.drawable.sym_def_app_icon) //图标 .setContentTitle("标题") .setContentInfo("右下角") .setContentText("内容") .setAutoCancel(true) //点击一下就消失 .setContentIntent(pendingIntent) //延迟意图 .setTicker("刚出来是在手机最上方显示,一会就消失") .setWhen(System.currentTimeMillis()) //消息的时间 .build(); //创建notification // notification.flags = Notification.FLAG_AUTO_CANCEL; 跟setAutoCancel一样作用 //显示notification 第一个参数不能重复,否则就只能显示重复的最后一个notification manager.notify(1,notification);
3. 自定义notification
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main); remoteViews.setImageViewResource(R.id.imageView,android.R.drawable.sym_def_app_icon); remoteViews.setTextViewText(R.id.textText,"textview 的内容"); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, DD.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setAutoCancel(true) //点击一下就消失 .setContentIntent(pendingIntent) //延迟意图 .setContent(remoteViews) //设置自定义的view .build(); manager.notify(1,notification);
时间: 2024-10-22 00:25:33