屏幕锁定时如何通知推送通知?
Posted
技术标签:
【中文标题】屏幕锁定时如何通知推送通知?【英文标题】:How to notify about the push notification when screen is locked? 【发布时间】:2015-05-16 13:45:45 【问题描述】:大家好, 我是 android 开发新手,目前正在处理谷歌云消息,一切正常,但问题是我无法在锁定屏幕上获取应用程序定义的通知图标和颜色,就像其他应用程序给出的一样,比如什么是应用程序绿色和图标在锁定屏幕上显示类似 gmail 红色和主屏幕上显示的图标,所以任何人都可以告诉我如何实现它,我非常感谢你。
private void sendNotification(String msg)
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setColor(
getResources().getColor(
R.color.abc_primary_text_material_dark))
.setVibrate(new long[] 1000, 1000, 1000, 1000, 1000 )
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentInfo("hi")
.setTicker("hi")
.setPriority(Notification.PRIORITY_HIGH)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setLights(0x0000FF, 1000, 4000)
;
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm
.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),
"TAG");
wakeLock.acquire(15000);
// Settings.System.putString(getApplicationContext().getContentResolver(),
// Settings.System.NEXT_ALARM_FORMATTED, "hi");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
【问题讨论】:
请提供您尝试的代码。 连灯都不工作。 【参考方案1】:Notification notif = new Notification(
R.drawable.ic_launcher, message,
System.currentTimeMillis());
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context, WelcomeActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notif.setLatestEventInfo(context, title, message, contentIntent);
【讨论】:
【参考方案2】:在您的代码中,它是通过setSmallIcon
设置的图标。代码中的值R.drawable.ic_launcher
指的是您的Android 项目的res/drawable 目录中的图像。您可以将其更改为R.drawable.<name of the icon you want to use>
,只要该图像已放置在可绘制文件夹中即可。
【讨论】:
以上是关于屏幕锁定时如何通知推送通知?的主要内容,如果未能解决你的问题,请参考以下文章
打开应用程序并锁定屏幕时未收到 Ionic FCM 推送通知,除非被点击