无法在离子框架 GCM 中接收通知
Cannot receive notifications in ionic framework GCM
我在 android 中使用带有离子框架的 ngcordova 推送插件。
我的设备正在 GCM
中注册。我也得到了注册 ID。我能够将 regid
存储在用户 table 中。到目前为止一切正常。但是当我从我的 php 服务器发送通知时,我无法在我的手机 phone 中收到通知。我得到的 gcm 对 php 代码 的响应是:
{"multicast_id":5214502018855543648,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439022666580761%156ac804f9fd7ecd"}]}
我的 JavaScript 代码是。
.run(function($ionicPlatform,ngFB,$state,$cordovaPush,$rootScope) {
$ionicPlatform.ready(function() {
// ------------------------------ GCM NG-CORDOVA PUSH-----------------------------------
var androidConfig = {
"senderID": "XXXXXXXXXX"
};
$cordovaPush.register(androidConfig).then(function(result) {
// Success
alert(result);
}, function(err) {
// Error
alert(err);
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
$rootScope.reg_id = notification.regid;
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
// WARNING: dangerous to unregister (results in loss of tokenID)
// $cordovaPush.unregister(options).then(function(result) {
// Success!
//}, function(err) {
// Error
//})
});
})
我的 php 代码是:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
我不明白我哪里错了。请帮助我。
谢谢
我曾经遇到过同样的问题,我建议您使用下面的 link 制作您的第一个应用程序,然后让第二个 regid 确保设备正确,保存完好并且应用程序的密钥是正确的。尝试使用相同的浏览器发送通知,有时时间会有所不同。
确保将 $cordovaPush.register() 和 $rootScope.$on() 函数包装在 document.addEventListener("deviceready", function() {}
像这样
document.addEventListener("deviceready", function() {
console.log("insode ");
$cordovaPush.register(androidConfig).then(function(result) {
console.log(result);
// Success
}, function(err) {
console.log(err);
// Error
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
console.log(event);
console.log(notification);
switch (notification.event) {
case 'registered':
if (notification.regid.length > 0) {
//alert('registration ID = ' + notification.regid);
}
$http({
url: "http://192.168.1.101/saveregid.php",
method : "POST",
data : {
id : notification.regid
}
}).success(function(){
alert("Id is saved");
});
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
$scope.notification = notification;
//alert('message = ' + notification + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
}, false);
为了获得正确的注册 ID,我在 http://192.168.1.101/saveregid.php 制作了一个简单的脚本,它在 post 请求中获取注册 ID,并将其存储在我可以访问的服务器文件中。从服务器文件中获取准确的注册 ID,然后尝试发送通知。
我在 android 中使用带有离子框架的 ngcordova 推送插件。
我的设备正在 GCM
中注册。我也得到了注册 ID。我能够将 regid
存储在用户 table 中。到目前为止一切正常。但是当我从我的 php 服务器发送通知时,我无法在我的手机 phone 中收到通知。我得到的 gcm 对 php 代码 的响应是:
{"multicast_id":5214502018855543648,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439022666580761%156ac804f9fd7ecd"}]}
我的 JavaScript 代码是。
.run(function($ionicPlatform,ngFB,$state,$cordovaPush,$rootScope) {
$ionicPlatform.ready(function() {
// ------------------------------ GCM NG-CORDOVA PUSH-----------------------------------
var androidConfig = {
"senderID": "XXXXXXXXXX"
};
$cordovaPush.register(androidConfig).then(function(result) {
// Success
alert(result);
}, function(err) {
// Error
alert(err);
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
$rootScope.reg_id = notification.regid;
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
// WARNING: dangerous to unregister (results in loss of tokenID)
// $cordovaPush.unregister(options).then(function(result) {
// Success!
//}, function(err) {
// Error
//})
});
})
我的 php 代码是:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
我不明白我哪里错了。请帮助我。
谢谢
我曾经遇到过同样的问题,我建议您使用下面的 link 制作您的第一个应用程序,然后让第二个 regid 确保设备正确,保存完好并且应用程序的密钥是正确的。尝试使用相同的浏览器发送通知,有时时间会有所不同。
确保将 $cordovaPush.register() 和 $rootScope.$on() 函数包装在 document.addEventListener("deviceready", function() {} 像这样
document.addEventListener("deviceready", function() {
console.log("insode ");
$cordovaPush.register(androidConfig).then(function(result) {
console.log(result);
// Success
}, function(err) {
console.log(err);
// Error
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
console.log(event);
console.log(notification);
switch (notification.event) {
case 'registered':
if (notification.regid.length > 0) {
//alert('registration ID = ' + notification.regid);
}
$http({
url: "http://192.168.1.101/saveregid.php",
method : "POST",
data : {
id : notification.regid
}
}).success(function(){
alert("Id is saved");
});
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
$scope.notification = notification;
//alert('message = ' + notification + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
}, false);
为了获得正确的注册 ID,我在 http://192.168.1.101/saveregid.php 制作了一个简单的脚本,它在 post 请求中获取注册 ID,并将其存储在我可以访问的服务器文件中。从服务器文件中获取准确的注册 ID,然后尝试发送通知。