推送通知未在 ionic 中显示
Posted
技术标签:
【中文标题】推送通知未在 ionic 中显示【英文标题】:Push notification not showing in ionic 【发布时间】:2016-04-27 07:52:17 【问题描述】:我正在 ionic 框架中进行推送通知。我只有警觉。我需要收到通知。
我的 javascript 代码
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ionic.service.core','ngCordova', 'ionic.service.push'])
.run(function($ionicPlatform)
$ionicPlatform.ready(function()
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
if(window.StatusBar)
StatusBar.styleDefault();
);
)
.config(['$ionicAppProvider', function($ionicAppProvider)
$ionicAppProvider.identify(
app_id: '38676e5c',
api_key: '76e051384cb6095e751db3791e26b2bd1162cceccd039f2a',
dev_push: true
);
])
.controller('PushCtrl', function($scope, $rootScope, $ionicUser, $ionicPush)
$rootScope.$on('$cordovaPush:tokenReceived', function(event, data)
alert('Success: ' + data.token);
console.log('name: ' + data.token + "--- Id: ");
console.log('Got token: ' , data.token, data.platform);
$scope.token = data.token;
);
$scope.identifyUser = function()
var user = $ionicUser.get();
if (!user.user_id)
user.user_id = $ionicUser.generateGUID();
angular.extend(user,
name: 'My Name',
bio: 'I am awesome'
);
$ionicUser.identify(user).then(function()
$scope.identified = true;
console.log('name: ' + user.name + "--- Id: " + user.user_id);
);
;
$scope.pushRegister = function()
$ionicPush.register(
canShowAlert: true,
canSetBadge: true,
canPlaySound: true,
canRunActionsOnWake: true,
onNotification: function(notification)
// handle your stuff
return true;
);
;
);
我的服务器代码
curl -u b81bb9cdc8ed06236804524c8e9b59ccc001900cd22f3c65: -H "Content-Type: application/json" -H "X-Ionic-Application-Id: 38676e5c" https://push.ionic.io/api/v1/push -d '"tokens": ["DEV-83f6de08-18fc-4bd9-9acd-72f974c2958a"],"production": false, "notification": "alert":"asasdsasd", "title": "demo", "android": "payload": "title": "hello","message": "hello", "ios": "payload": "title": "hello","message": "hello"'
我的输出
My output
我的问题是
我只收到警报(不是通知) 设备令牌每秒都在变化..
我需要什么??
我需要在通知栏中获取推送通知..
我需要唯一的设备令牌..(id)..
【问题讨论】:
***.com/questions/36762552/…查看这个堆栈问题并在此处查看教程devdactic.com/android-push-notifications 为了获得设备令牌,请参考***.com/questions/36717556/… 谢谢@mohan ...我也试过了..它会抛出这个错误“Ionic.User.anonymousId(); is undefined”,并将一些虚拟数字设置为用户ID。它不会在 ionic.io 中创建用户 ..如果你有输出,请将该包发送给我(balakumaran.raji@gmail.com) 在你的问题中你问你需要设备令牌堆栈溢出作为解决方案,然后要获得离子推送通知你需要 api 和 sederid 那么你的错误是什么 查看我的输出...我收到警报格式的通知..我没有收到通知...这是我的问题..我尝试了所有方法我没有得到任何输出跨度> 【参考方案1】:在你的 ionic app.js 控制器中试试这个
.run(function($ionicPlatform)
$ionicPlatform.ready(function()
if(window.cordova && window.cordova.plugins.Keyboard)
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
if(window.StatusBar)
StatusBar.styleDefault();
//Ionic.io();
var push = new Ionic.Push(
"debug": true
);
push.register(function(token)
console.log(token);
console.log("Device token:",token.token);
tokenapp = token.token;
console.log(tokenapp);
);
);
)
【讨论】:
它抛出“错误:未定义离子” 你有没有将你的发件人ID和服务器密钥注册到你当前的项目文档中 像这样 ionic push --google-api-key your-google-api-key, ionic config set gcm_key your-gcm-project-number, ionic config set dev_push false 我已经发送了一封邮件,将您的项目作为 zip 文件发送,我将检查它是否将 sederid 和 api 服务器密钥也发送到该邮件 对不起,我没有收到任何人以上是关于推送通知未在 ionic 中显示的主要内容,如果未能解决你的问题,请参考以下文章