javascript - Phonegap / Cordova Push Notification not waking up screen
问题描述
I've been doing some research and android
seems a bit stricter on waking up screen (light up device screen) when push notification is received.
What I would like to achieve is like a text message notification that it would turn on the screen, sound and vibrate. But my push notification only chime or vibrate. Is waking up the device from sleeping possible in cordova? I am using pubnub for the backend.
Here's my sample fcm payload:
var pushPayload = {
"message": "Some message",
"user_id": "1",
"pn_gcm" : {
"priority" : "high",
"data" : {
"title":"Notification title",
"body":"You are a winner!",
"room" : "Room name",
//"count" : 5,
"content-available":"1",
"force-start": "1",
"priority":2
}
}
};
And here's my piece of AndroidManifest.xml
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
I am using phonegap-plugin-push
.
解决方案
好的,我最终创建了自己的科尔多瓦插件来处理唤醒屏幕。这是我在插件中使用的代码:
Context context = this.cordova.getActivity().getApplicationContext();
PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
boolean result= Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isInteractive()|| Build.VERSION.SDK_INT< Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isScreenOn();
if (!result){
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);
}`
所以,在notification
事件中,我这样调用我的插件:
` push.on('通知', function(data) {
//call wakeup screen
window.plugins.wakeUpScreen.wakeup(function() {
console.log('Wake up!');
}, function(err) {
console.log('Wake up error: ' + err);
});
}); `
推荐阅读
- c# - 相同的 Web API 返回多种格式
- java - Java:即使使用字符串也如何继续
- angular - 角度通用的窗口未定义错误| 角 10 | ng 10
- node.js - 如何在 EJS/DJS 中拉取所有成员服务器?
- c# - 在`ReadAsStringAsync`之后是否需要检查`IsSuccessStatusCode`?
- azure - 无法在 VS 的 CDC 控制任务中将 azure sql db 连接为连接管理器
- python - 无法安装特定的 numpy 版本
- java - API 中的 LocalDate 作为 javax.ws.rs.QueryParam
- c# - 如何在日志中打印 JTone 和 C# 对象的内容
- simulation - AnyLogic中接送功能的视觉增强