gpt4 book ai didi

android - 推送通知在 sleep 模式下不显示

转载 作者:太空狗 更新时间:2023-10-29 15:07:32 24 4
gpt4 key购买 nike

我在 GCMIntentService.java 中使用以下代码

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
wl.acquire();

但推送通知在 sleep 模式下不显示。

当我解锁手机时推送通知工作正常,然后正确接收通知。

但我想在 sleep 模式下显示通知,就像播放声音和接收来自 gmail 应用程序的通知一样。请帮助我该怎么做。

@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message" + intent.getStringExtra("message"));
String message = intent.getStringExtra("message");// getString(R.string.gcm_message);
generateNotification(context, message);
}

private static void generateNotification(Context context, String message) {
int icon = R.drawable.logo;
long when = System.currentTimeMillis();


NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Tabs.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;


notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(0, notification);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);



}

最佳答案

试试这个:

    private static void generateNotification(Context context, String message) {
int icon = R.drawable.notification_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// Log.i(TAG, "Received reg id:=>"+regId);
Intent notificationIntent = new Intent(context, Message.class);
notificationIntent.putExtra("Message", message);

// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;

// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Constant.iMessageParsing = 0;

}

在 GCMIntentService 类的 onMessage(Context context, Intent intent) 方法中调用此方法。

试试这个代码::

PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);

if(isScreenOn==false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(10000);
}

关于android - 推送通知在 sleep 模式下不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20465490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com