use*_*669 28 notifications android lockscreen
我一直致力于推送通知,我能够实现它并在状态栏上显示它,我面临的问题是我想显示它,即使手机是锁定的,在它所说的锁定屏幕下("拖动解锁"),我已经看过这样的通知,但无法找到任何示例.
示例: 就像您收到未接来电一样,它会在屏幕上的锁定按钮下显示.
码:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon_launcher;
CharSequence tickerText = "MyApplication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;;
CharSequence contentTitle = this.title;
CharSequence contentText = this.message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTICE_ID, notification);
Run Code Online (Sandbox Code Playgroud)
Kir*_*ela 17
使用NotificationCompat.Builder创建通知
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
锁定屏幕上的推送通知 http://www.hongkiat.com/blog/android-lock-screen-notifications/
使用 NotificationCompat.Builder 创建通知,但请确保向公众公开,例如
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setContentTitle("Title")
.setContentText("content")
.setSmallIcon(R.mipmap.ic_launcher)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81163 次 |
| 最近记录: |