简体   繁体   English

Phonegap / Cordova 推送通知未唤醒屏幕

[英]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.我一直在做一些研究,当收到推送通知时, android在唤醒屏幕(点亮设备屏幕)方面似乎有点严格。

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?在cordova中可以从睡眠中唤醒设备吗? I am using pubnub for the backend.我正在使用 pubnub 作为后端。

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 } }
};
这是我的 fcm 负载示例: 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 } }
};
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" />这是我的 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 .我正在使用phonegap-plugin-push

Ok, I ended up creating my own cordova plugin just to handle on waking up screen.好吧,我最终创建了自己的cordova插件来处理唤醒屏幕。 And here's the code that I used in my plugin:这是我在插件中使用的代码:

    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);
    }`

So, in the notification event, I called my plugin like this:所以,在notification事件中,我这样调用我的插件:

` push.on('notification', function(data) { ` push.on('通知', 函数(数据) {

    //call wakeup screen
    window.plugins.wakeUpScreen.wakeup(function() {
      console.log('Wake up!');
    }, function(err) {
      console.log('Wake up error: ' + err);
    });          

}); `

Android devices have their own notification preferences and some cannot be overridden by your app. Android 设备有自己的通知首选项,有些不能被您的应用覆盖。 The "Ambient Display" setting will have the screen wake upon notification, but this is a feature that must be turned on in the phone settings. “环境显示”设置将在通知时唤醒屏幕,但这是一项必须在手机设置中打开的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM