我正在使用cordova pushPlugin
我的app.js中的代码位于$ionicPlatform.ready
下,
$cordovaPush.register(androidConfig).then(function(result) {
// Success
// console.log(result);
}, function(err) {
// Error
// console.log(err);
});
$rootScope.$on('$cordovaPush:notificationReceived',
function(event, notification) {
console.log('event',notification.event)
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
//alert('registration ID = ' + notification.regid);
window.localStorage['push_token'] = notification.regid;
if(window.localStorage.getItem('id') != null){
console.log(3, 'api')
var addDevicetoken = myConfig.ApiBaseUrl+'api/addDeviceToken';
var postData = {
'id' : window.localStorage['id'],
'device_token' : notification.regid,
'device_type' : 0, // android
'uuid' : uuid
};
appRequestApi.requestPost(addDevicetoken,postData).then(function(result){
console.log(result);
});
}
}
break;
case 'message':
console.log(notification);
// 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;
}
});`
当我的应用处于后台时,不会触发消息事件。
我也是从laravel发送有效载荷
$push = new PushNotification('fcm');
$push->setMessage([
'data' => [
'title' => $title,
'body' => $message,
'content-available' => 1,
'notification_type' => $notification_type,
'icon' => 'ic_launcher',
]
])
->setDevicesToken($tokens)
->send();
// dd($push->getFeedback());
return true;
请帮忙