如何在锁定屏幕上显示android推送通知?

时间:2018-06-08 05:59:47

标签: android push-notification notifications

我面临在锁定屏幕上显示推送通知的问题。我能够收到通知但无法在锁定屏幕上显示它们。

Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            //.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.drawable.loader)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) //to show content on lock screen
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .build();

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count, notification);

我也尝试过使用PowerManager,但它只是唤醒了锁定屏幕,但没有在屏幕上显示通知。

PowerManager powerManager = (PowerManager) mContext.getSystemService(POWER_SERVICE);

if (!powerManager.isInteractive()){ // if screen is not already on, turn it on (get wake_lock for 10 seconds)
    PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MH24_SCREENLOCK");
    wl.acquire(10000);
    PowerManager.WakeLock wl_cpu = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MH24_SCREENLOCK");
    wl_cpu.acquire(10000);
}

由于我是Android的新手,我无法解决问题。请帮帮我。

0 个答案:

没有答案