首页 > 解决方案 > 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.

标签: javascriptandroidcordovaphonegaphtml-framework-7

解决方案


好的,我最终创建了自己的科尔多瓦插件来处理唤醒屏幕。这是我在插件中使用的代码:

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

}); `

推荐阅读