ionic 前端接收到后台推送來的消息后,顯示在手機的通知欄上


這里主要用到cordova提供的插件:(在app沒有關閉的情況下只要有推送的消息就會有提醒,但是在app關閉的情況下就不會提示)

首先安裝cordova-plugin-local-notifications,然后在JS配置好如下就可以了:

安裝:ionic/cordova plugin add cordova-plugin-local-notifications;
安裝的時候它會自動安裝幾個依賴的插件;

狀態欄通報提醒  
$scope.notificationReminder function(faxInfo){  
    cordova.plugins.notification.local.add({    
        id: id,  
        title: '',    
        text: '',    
        at: new Date().getTime(),    
        //badge: 2,    
        autoClear: true,//默認值    
        sound: 'res://platform_default',//默認值    
        icon: 'res://ic_popup_reminder', //默認值    
        ongoing: false  //默認值    
    });  
}
  以上的屬性:    

 

 此時ionic build android 報錯:

原因如下:

cordova 5.0.0以上版本對  evaluateJavascript  不再支持,用sendJavascript進行替換。

在插件目錄下的LocalNotification.java文件中:

561行的代碼替換如下:

webView.getView().post(new Runnable(){  
          public void run(){  
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
              webView.sendJavascript(js);  
            } else {  
              webView.loadUrl("javascript:" + js);  
            }  
          }  
        });  

 此時在cordova build android,又出現錯誤,如下圖:

解決方法:

網上搜了哈:報錯的原因是:依賴包重復;

個錯誤在app的build.gradle里面添加下面這句就好了

android {
   
    defaultConfig {
        ...
        multiDexEnabled true
    }

}

 此時我又重新build,又報錯,如下:

 為什么呢?    重復添加了包,重復引用了IdRes.class;

解決方案:

 事件監聽:

//shedule事件在每次調用時觸發  
cordova.plugins.notification.local.on('schedule', function (notification) {  
    alert('scheduled:' + notification.id);  
});  
//通知觸發事件  
cordova.plugins.notification.local.on('trigger', function (notification) {  
    //alert('triggered:' + notification.id);  
    alert(JSON.stringify(notification));  
});  
//監聽點擊事件(點擊通知欄上的消息之后觸發的事件,頁面邏輯可以在這里寫)
cordova.plugins.notification.local.on('click', function (notification) {  
    alert(JSON.stringify(notification));  
    document.getElementById('title').innerHTML = JSON.stringify(notification.data);  
}); 

 

      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM