简体   繁体   English

phonegap 3.3.0 中的本地通知

[英]Local notification in phonegap 3.3.0

Thank you Dawson!谢谢道森! The problem mentioned below has been solved in android.下面提到的问题已经在android中解决了。 But sadly it does not work when I build the project in ios, could anyone help?但遗憾的是,当我在 ios 中构建项目时它不起作用,有人可以帮忙吗?

I am going to using this plugin to build a android/ios apps with local notification.我将使用这个插件来构建一个带有本地通知的 android/ios 应用程序。 https://github.com/katzer/cordova-plugin-local-notifications/wiki https://github.com/katzer/cordova-plugin-local-notifications/wiki

First I created a phonegap project首先我创建了一个phonegap项目

phonegap create notification

Add the plugin添加插件

cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git

Modify the /www/config.xml修改/www/config.xml

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

I copied local-notification.js to /www dir and referenced it in the index.html我将 local-notification.js 复制到 /www 目录并在 index.html 中引用它

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
</head>
<body>
    <div class="app">
        <h1>PhoneGap</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="local-notification.js"></script>
    <script type="text/javascript">]
        window.plugin.notification.local.add({ message: 'Great app!' });
    </script>
</body>

I built the android apps using the command我使用命令构建了 android 应用程序

phonegap build android

I can successfully built the android apps but there is no notification poped up as expected.我可以成功构建 android 应用程序,但没有按预期弹出通知。 Can anyone know whats wrong with me at the above seetting?任何人都可以知道我在上面的设置中有什么问题吗? Many thanks to all of you.非常感谢你们所有人。

You wont be able to call that function until after the deviceReady event has fired.在 deviceReady 事件触发之前,您将无法调用该函数。

For testing I always take my plugin function and add it as a click action to the "device is ready" p tag.为了测试,我总是使用我的插件功能并将其作为点击操作添加到“设备准备就绪”p 标签。

Example:例子:

<p class="event received">Device is Ready</p>

change to:改成:

<p class="event received" onclick="window.plugin.notification.local.add({ message: 'Great app!' });">Device is Ready</p>

I do this with the default cordova example app since I know that 'Device is ready' will not display until after the deviceReady event has fired.我使用默认的cordova 示例应用程序执行此操作,因为我知道“设备准备就绪”直到 deviceReady 事件触发后才会显示。 This is a good way to test if plugins are working before you do any real work on your project.这是在您对项目进行任何实际工作之前测试插件是否正常工作的好方法。

This is another example.这是另一个例子。 It has custom sound and other properties such as title, badge type is interesting how this plugin doc is not finished yet, creating local notifications for android working absolutely beautiful with latest cordova (3.4.x) just create a var and assign your package name (the one you use when created the phonegap/cordova project with the command line something like this for example:它具有自定义声音和其他属性,例如标题,徽章类型很有趣,这个插件文档尚未完成,为使用最新的cordova(3.4.x)创建绝对漂亮的android本地通知只需创建一个var并分配您的包名称(您在使用命令行创建 phonegap/cordova 项目时使用的那个,例如:

     cordova create LocalNotification com.example.localnotification LocalNotification

Should be used in the plugin with these values:应在具有以下值的插件中使用:

    var package_name = "com.example.localnotification";
                    window.plugin.notification.local.add({
                                  date        : Math.round(new Date().getTime()/1000 + 5),
                                  title       : "Android App Tes Local Notification", 
                                  message       : "This is a new local notification.",
                                  repeat        : "daily",
                                  sound       : 'android.resource://' + package_name + '/raw/beep',
                                  badge           : 0,
                                  id             : 666,
                                  foreground      : function(notificationId){
                                  console.log("Hello World! This alert was triggered by notification " + notificationId);
                                  },
                                  background  : function(notificationId){
                                  console.log("Hello World! This alert was triggered by notification " + notificationId);
                                  }           
                                  });

Download local notificator plugin (working on Android ONLY) 下载本地通知器插件(仅适用于 Android)

Download beep.mp3下载哔.mp3

stack overflow original comment堆栈溢出原始注释

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

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