首页 文章

Cordova Phonegap本地通知不起作用

提问于
浏览
9

我想首先说这是一个非常好的插件(https://github.com/katzer/cordova-plugin-local-notifications),但在使用它时遇到一些困难 .

我正在使用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" />

然而通知没有出现 - 电话没有任何反应 - 没有,nada,zilch . 我还下载了KitchenSink App(https://github.com/katzer/cordova-plugin-local-notifications/tree/example)并安装在Phonegap build和我的手机上,没有再发生任何事情..

这是我在index.html上的代码,所以当手机触发它时应该注册本地通知asap:

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

How can I get local notifications to appear in Phonegap?

2 回答

  • 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

    Answer 1 :for version 3.5.0

    看看plugin's plugin.xml . 见第22行

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

    这意味着插件 only supports version greater than 3.6.0 并且您使用的是3.5.0

    Answer 2 :for version 5.0 or higher

    请尝试以下代码 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>
    

相关问题