Ionic 2推送通知错误

时间:2016-06-04 07:35:55

标签: android ionic2

我在下面使用了这段代码

React.createClass()

}

Result Mobile 但是,当我尝试这个代码时,我得到了移动设备,但我只得到警报显示,但我没有收到通知显示.. 我把我的手机屏幕贴了..

所以你可以给我发送离子2推送通知代码......

1 个答案:

答案 0 :(得分:1)

我写了一篇文章来解释iOS和Android的推送通知。看看它是否有用Ionic 2 Push Notifications

目前,我无法在Ionic 2网站上找到任何有关推送通知的文档。但是使用此链接Ionic 2 PushPhonegap plugin push,我至少可以在Android上获得基本通知。

我在MainApp构造函数中有以下代码

UPDATE FD DCS
  SET DCS.F_DISTANCE =
    (SELECT FD.F_DISTANCE
    FROM TMP FD
    WHERE FD.DATE_SID = DCS.DATE_SID
    AND FD.SID = DCS.SID
    AND FD.DA_SID = DCS.DASID
    AND FD.AA_SID = DCS.AA
    AND FD.F_DISTANCE IS NOT NULL
    )
WHERE DCS.BATCH_ID=BATCH_ID;

在服务器端,我使用以下代码进行推送通知

 constructor(platform:Platform, private app:IonicApp) {
        platform.ready().then(() => {
            StatusBar.styleDefault();
        });

        var push = Push.init({
            android: {
                senderID: "YOUR_SENDER_ID"
            },
            ios: {
                alert: "true",
                badge: true,
                sound: 'false'
            },
            windows: {}
        });

        push.on('registration', (data) => {
            console.log("registraiton id " + data.registrationId);
        });

        push.on('notification', (data) => {
            console.log(data.message);
            console.log(data.title);
            console.log(data.count);
            console.log(data.sound);
            console.log(data.image);
            console.log(data.additionalData);
        });

        push.on('error', (e) => {
            console.log(e.message);
        });

}

我在Ionic网站上为推送通知文档启动了一个帖子Ionic 2 Push Thread,如果需要,您可以关注该主题。

运行服务器的步骤。在OS X上,您可能需要使用var express = require('express'); var gcm = require('node-gcm'); var app = express(); var gcmApiKey = 'YOUR_GCM_API_KEY'; // GCM API KEY OF YOUR GOOGLE CONSOLE PROJECT var server = app.listen(3000, function () { console.log('server is just fine!'); }); app.get('/', function (req, res) { res.send("This is basic route"); }); app.get('/push', function (req, res) { var device_tokens = []; //create array for storing device tokens var retry_times = 4; //the number of times to retry sending the message if it fails var sender = new gcm.Sender(gcmApiKey); //create a new sender var message = new gcm.Message(); //create a new message message.addData('title', 'PushTitle'); message.addData('message', "Push message"); message.addData('sound', 'default'); message.collapseKey = 'Testing Push'; //grouping messages message.delayWhileIdle = true; //delay sending while receiving device is offline message.timeToLive = 3; //number of seconds to keep the message on //server if the device is offline //Take the registration id(lengthy string) that you logged //in your ionic v2 app and update device_tokens[0] with it for testing. //Later save device tokens to db and //get back all tokens and push to multiple devices device_tokens[0] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; sender.send(message, device_tokens[0], retry_times, function (result) { console.log('push sent to: ' + device_tokens); res.status(200).send('Pushed notification ' + device_tokens); }, function (err) { res.status(500).send('failed to push notification '); }); }); 运行以下命令。

  • NodeJs下载Nodejs。它将在您的系统中安装node和npm。
  • sudo
  • npm install express-generator -g
  • express MySampleApp
  • cd MySampleApp
  • npm install --save node-gcm
  • 使用上述服务器代码更改app.js的内容,然后使用以下命令
  • 运行服务器
  • npm install