PhoneGap / Cordova应用程式通知 [英] PhoneGap/Cordova app notifications

查看:143
本文介绍了PhoneGap / Cordova应用程式通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我是新加入PhoneGap / Cordova的推送通知 -


  2. 本地通知 - 在设置的时间间隔(日期和时间),我可以提示用户我的应用程序的最新文章。


我进行了大量的搜索,但无法找到一个可以直接导入项目的工作示例,然后修改。



我试过下面的插件,但无法使它工作
https://github.com/katzer/cordova-plugin-local-notifications/

解决方案

在项目中启用推送通知的步骤。


  1. =https://console.developers.google.com/ =nofollow> https://console.developers.google.com/ 输入您的专案名称。


  2. 请参考下面的链接来安装推送插件
    https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md


  3. 您的push.js档案中的程式码
    请参阅 https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md



    / / code:推送通知初始化和注册。

      if(!$ window.PushNotification){
    return;
    }
    var push = $ window.PushNotification.init({
    android:{
    senderID:包含您的应用程式发件人ID XXXXXXX
    },
    ios:{
    alert:true,
    badge:true,
    sound:'false'
    },
    windows:{}
    } );

    push.on('registration',function(data){
    //这给出了唯一的deviceId,或者你可以维护deviceIds数组来注册多个设备
    //如果指定了单个deviceId例如
    //在数据库中保存此deviceId以进行进一步操作,即将消息推送到这些ID
    console.log(data.registrationId);
    } );

    push.on('notification',function(data){
    console.log(data.message);
    });

    push.on('error',function(e){
    console.log(e.message);
    }); {
    console.log('off notify');
    });


  4. 有几种方式发送推送通知。这里我采取gcm服务器的帮助通知。
    您将需要安装node-gcm。
    创建一个新的server.js文件。

      var gcm = require('node-gcm'); 
    var message = new gcm.Message();
    // API服务器密钥
    var sender = new gcm.Sender('GIVE_YOUR_SERVER_API_KEY');
    var registrationIds = [];

    //为要发送的有效负载数据赋值...
    message.addData('message',\\\✌ Peace,Love \\\❤和PhoneGap \\\✆!
    message.addData('title','Push Notification Sample');
    message.addData('msgcnt','3'); //在状态栏的通知中显示
    message.addData('soundname','beep.wav'); //声音播放
    message.timeToLive = 3000;

    //至少需要一个注册ID
    //这里使用注册ID



    sender.send(message,mail)。



    registrationIds.push('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); registrationIds,4,function(result){
    console.log(result);
    });



$ b b

请参阅 http:/ /devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/ ,以便清楚了解通过gcm-server发送通知。这将在设备上显示通知。



您也可以使用firebase而不是gcm。



启用本地通知的步骤:
使用 cordova插件将插件添加到项目中add https://github.com/katzer/cordova-plugin-local-notifications



调用本地通知,如链接 https://github.com/katzer/cordova-plugin-local-notifications/


I am new to PhoneGap/Cordova, i am looking to add some notifications to my app.

  1. Push notification - so when a new article is released on app it will alert the users.

  2. Local Notifications - On set intervals (date and time) I can prompt the user of the latest articles on my app.

I had done a lot of searching but unable to find a working example that I can import straight into a project and then modify.

I have tried the following plugin in but was unable to get it working https://github.com/katzer/cordova-plugin-local-notifications/

解决方案

Steps for enabling push notifications in project.

  1. create a project in https://console.developers.google.com/ with your project name.

  2. Refer the below link for push plugin installation https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md

  3. Code in your push.js file Refer https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md

    //code: push notification initialization and registration.

    if (!$window.PushNotification) {
    return;
    }
     var push = $window.PushNotification.init({
    android: {
        senderID: "Include your app sender ID XXXXXXX" 
    },
    ios: {
        alert: "true",
        badge: true,
        sound: 'false'
    },
    windows: {}
    });
    
    push.on('registration', function(data) {
    // this gives the unique deviceId, Or you can maintain array of deviceIds to register multiple devices.
    // if have specified single deviceId for example
    // save this deviceId in database for further operations, i.e. push messages to those ids.
    console.log(data.registrationId);
    });
    
    push.on('notification', function(data) {
    console.log(data.message);
    });
    
    push.on('error', function(e) {
    console.log(e.message);
    });
    push.off('notification', function(e) {
    console.log('off notify');
     });
    

  4. There are several ways to send push notification. Here I'm taking help of gcm-server to notify. You will need to install node-gcm. Create a new server.js file.

     var gcm = require('node-gcm');
     var message = new gcm.Message();
    //API Server Key
     var sender = new gcm.Sender('GIVE_YOUR_SERVER_API_KEY');
    var registrationIds = [];
    
     // Value the payload data to send...
    message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
    message.addData('title','Push Notification Sample' );
    message.addData('msgcnt','3'); // Shows up in the notification in the status bar
    message.addData('soundname','beep.wav'); //Sound to play 
    message.timeToLive = 3000;
    

    // At least one reg id required // Here use the registration Ids that you got during device registration.

    registrationIds.push('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

    sender.send(message, registrationIds, 4, function (result) { console.log(result); });

Refer http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/ for clear understanding of sending notification via gcm-server. This will show the notification on the device.

You can also make use of firebase instead of gcm.

Steps for enabling local notification: Add plugin to your project using cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications

Invoke the local notification as shown in the link https://github.com/katzer/cordova-plugin-local-notifications/

这篇关于PhoneGap / Cordova应用程式通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆