【发布时间】:2019-11-22 06:29:03
【问题描述】:
对于我的应用程序,我需要实现类似 WhatsApp 的行为,当设备被锁定时在锁定屏幕上方显示我的应用程序,并且我在 Android Q 以下的 android 版本中成功地做到了。为此,我授予 Settings.ACTION_MANAGE_OVERLAY_PERMISSION。没有SYSTEM_ALERT_WINDOW permission,有人知道怎么做吗?对于推送通知,我使用 fcm。
我的代码:
private void tryWakeUp() {
try {
String ns = getApplicationContext().getPackageName();
String cls = ns + ".MainActivity";
Intent intent = new Intent(getApplicationContext(), Class.forName(cls));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra("foreground", true);
intent.putExtra("incoming_call", true);
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl1 = pm.newWakeLock(
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE |
PowerManager.FULL_WAKE_LOCK,
"wl1"
);
wl1.acquire(10000);
Log.d(TAG, "try wake up");
startActivity(intent);
} catch (Exception e) {
Log.w(TAG, "Failed to open application on message receive", e);
}
}
此代码在我收到数据推送通知后执行。
【问题讨论】:
-
尝试使用我的旧答案:stackoverflow.com/questions/58723440/…
-
不幸的是,它对我不起作用
-
嗨@Denis,我发现Skype,whatapp,messenger做得很完美,但我不知道怎么做,和你的问题一样,你解决了吗?谢谢
标签: java android push-notification