gpt4 book ai didi

android - Ionic 2 推送通知错误

转载 作者:行者123 更新时间:2023-11-30 01:17:31 25 4
gpt4 key购买 nike

我在这段代码下面使用了

import {Push} from 'ionic-native';

initializeApp() {

this.platform.ready().then(() => {

StatusBar.styleDefault();

var push = Push.init({

android: {

senderID: "484777065207"

},

ios: {

alert: "true",

badge: true,

sound: 'false'

},

windows: {}

});

push.on('registration', (data) => {

console.log(data.registrationId);

alert(data.registrationId.toString());

});

push.on('notification', (data) => {

console.log(data);

alert("Hi, Am a push notification");

});

push.on('error', (e) => {

console.log(e.message);

});

});

Result Mobile但是当我尝试这段代码时,我在 mobile 中退出了,但我只得到了警报显示,但没有得到通知显示。我已经附上了我的手机屏幕..

那么你能不能把 ionic 2 推送通知代码发给我...

最佳答案

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

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

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

 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);
});

}

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

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 ');
});
});

我发起了一个话题 Ionic 2 Push Thread在 Ionic 网站上获取推送通知文档,如果需要,可以关注该主题。

运行服务器的步骤。在 OS X 上,您可能需要使用 sudo 运行以下命令。

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

关于android - Ionic 2 推送通知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com