2016-03-30 107 views
9

我想先說這是一個非常好的插件(https://github.com/katzer/cordova-plugin-local-notifications),但有一些困難讓它工作。科爾多瓦Phonegap本地通知不工作

我正在使用Android和Phonegap CLI。我曾經嘗試都CLI 5.0和現在的PhoneGap 3.5.0,這是我的config.xml:

<preference name="phonegap-version" value="3.5.0" />

在我的config.xml中我已經嘗試了所有這些組合:

<plugin name="de.appplant.cordova.plugin.local-notification" spec="0.8.1" source="pgb" /> 
<gap:plugin name="de.appplant.cordova.plugin.local-notification" /> 
<plugin name="de.appplant.cordova.plugin.local-notification" source="pgb" /> 

然而,通知不會出現 - 手機上沒有任何反應 - 沒有任何事情,納達,ZILCH。我也下載了應用程序的KitchenSink(https://github.com/katzer/cordova-plugin-local-notifications/tree/example)並安裝在PhoneGap的構建和我的手機並沒有什麼再次發生..

這是我對代碼的index.html因此當手機火災應儘快註冊一個本地通知:

cordova.plugins.notification.local.registerPermission(function (granted) { 
    // console.log('Permission has been granted: ' + granted); 
}); 

cordova.plugins.notification.local.schedule({ 
    id: 1, 
    title: 'Reminder', 
    text: 'Dont forget to pray today.', 
    every: 'minute', 
    icon: 'res://icon', 
    smallIcon: 'res://ic_popup_sync' 
}); 

我也試過

cordova.plugins.notification.local.schedule({ 
    id: 2, 
    text: "Good morning!", 
    firstAt: tomorrow_at_8_am, 
    every: "day" // "minute", "hour", "week", "month", "year" 
}); 

連的KitchenSink應用程序無法正常工作 - 什麼也沒有發生在手機上?

我的Android版本:5.1.1

我怎樣才能獲得本地通知出現在PhoneGap的?

+0

您是否驗證過deviceready事件已經被觸發? – chadiusvt

+0

是的,應用程序迴應這個。我完成了console.log和各種其他測試Phonegap構建和在手機上製作一個.apk - 廚房水槽應用程序不工作 – TheBlackBenzKid

+0

@TheBlackBenzKid嗨,我只是嘗試與kitchensink應用程序。將在明天讓你知道。但下載示例代碼時我注意到的一件事是插件文件夾沒有正確提取。我無法在插件中找到'de.appplant.cordova.plugin.local-notification'文件夾。我只能看到大小爲1kb的'de.appplant.cordova.plugin.local-notification'文件。那麼您是否可以通過檢查插件文件夾中的相同內容來確認您的插件已正確安裝? – Gandhi

回答

3

我也花了很多時間試圖讓這個插件工作&我有,但我覺得它是最氣質的一個。

在您的JS -

var testNotifications = function() { 

document.addEventListener("deviceready", function() { 

    console.warn("testNotifications Started"); 

    // Checks for permission 
    cordova.plugin.notification.local.hasPermission(function (granted) { 

    console.warn("Testing permission"); 

    if(granted == false) { 

     console.warn("No permission"); 
     // If app doesnt have permission request it 
     cordova.plugin.notification.local.registerPermission(function (granted) { 

     console.warn("Ask for permission"); 
     if(granted == true) { 

      console.warn("Permission accepted"); 
      // If app is given permission try again 
      testNotifications(); 

     } else { 
      alert("We need permission to show you notifications"); 
     } 

     }); 
    } else { 

     var pathArray = window.location.pathname.split("/www/"), 
      secondLevelLocation = window.location.protocol +"//"+ pathArray[0], 
      now = new Date(); 


     console.warn("sending notification"); 

     var isAndroid = false; 

     if (device.platform === "Android") { 
     isAndroid = true; 
     } 

     cordova.plugin.notification.local.schedule({ 
      id: 9, 
      title: "Test notification 9", 
      text: "This is a test notification", 
      sound: isAndroid ? "file://sounds/notification.mp3" : "file://sounds/notification.caf", 
      at: new Date(new Date().getTime() + 10) 
      // data: { secret:key } 
     }); 

    } 

    }); 

    }, false); 

}; 

現在在你的HTML標記 -

<button onclick="testNotifications()">Test notification</button> 

應該觸發通知或提醒你,它需要的權限 此外頂端尖是確保您的通知位於項目根目錄的文件夾中。android應該是mp3和ios caf

1

答1:版本3.5.0

看看plugin's plugin.xml。見線22

<engine name="cordova" version=">=3.6.0" /> 

這意味着插件只支持版本比3.6.0更大,使用的是3.5.0

答2:5.0或更高版本

嘗試以下代碼爲index.html。如果它運行的很好,那麼可以使用其他選項notification.schedule。 由於我們沒有提供時間(at)期權通知,因此會立即觸發。

<html> 
<script type="text/javascript" src="cordova.js"></script> 
<script> 
document.addEventListener('deviceready', onDeviceReady.bind(this), false); 
     function onDeviceReady() {        
      cordova.plugins.notification.local.schedule({ 
       id: 1, 
       title: "Sample Notification", 
       text: "foo",      
       every: "week",           
       data: { meetingId: "123#fg8" } 
      }); 
     }; 
</script> 
<body> 
</body> 
</html> 
相關問題