我有一个应用程序,它使用新的gcm api从自定义网络服务器接收推送通知。一切正常。但是,我也希望在锁定屏幕上显示通知。
我用Google搜索并找到了这篇文章,但它无济于事:Android how to show notification on screen
有没有简单的方法来实现这一目标?谢谢你的帮助!
编辑//
这是一些代码。
在GCMHandler中,我使用此代码段显示通知:
private void sendNotification() {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("message", message);
intent.putExtra("pbid", pbid);
intent.putExtra("alarm", alarm);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alarm ? getString(R.string.txt_alarm_message_title) : getString(R.string.txt_info_message_title))
.setContentText(message);
// play alarm tone
if (alarm) playRingtone();
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
在应用程序的主要活动中,我使用了上面帖子中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setContentView(R.layout.act_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
...
}
问题是,通知已显示,但在锁定屏幕上不可用。如果屏幕被锁定,则不会收到推送通知;(
答案 0 :(得分:-2)
尝试这样做会有效。
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Notification noti = new Notification.Builder(
getApplicationContext())
.setContentTitle("New mail from " + " ")
.setContentText(msg).setContentIntent(contentIntent)
.setDefaults(1).setSmallIcon(R.drawable.gcm_cloud).build();
PowerManager pm = (PowerManager) this
.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
mNotificationManager.notify(NOTIFICATION_ID, noti);
}