我正在使用cordova angular app并使用cordova-plugin-fcm插件但是当我发送通知我的应用程序自动关闭时,我已经尝试了很多但没有运气。 请按代码检查: 它在onNotificationCallback函数中不会触发,总是在successCallback中触发。
FCMPlugin.onNotification(
function(data){ alert("Tapped: " + JSON.stringify(data) );
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert("Tapped: " + JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert("Not tapped: " + JSON.stringify(data) );
}
},
function(msg){
alert('onNotification callback successfully registered: ' + msg);
console.log('onNotification callback successfully registered: ' + msg);
},
function(err){
alert('Error registering onNotification callback: ' + err);
console.log('Error registering onNotification callback: ' + err);
}
);
$scope.send = function(){
//FCMPlugin.subscribeToTopic( topic, successCallback(msg), errorCallback(err) );
//All devices are subscribed automatically to 'all' and 'ios' or 'android' topic respectively.
//Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}".
FCMPlugin.subscribeToTopic('all'); //subscribe current user to topic
var fcm_server_key = "AIzaSyCmu7RXJkSsNJch9MB5ySDUbguyRBeAWm8";
$http({
method: "POST",
dataType: 'jsonp',
headers: {'Content-Type': 'application/json', 'Authorization': 'key=' + fcm_server_key},
url: "https://fcm.googleapis.com/fcm/send",
data: JSON.stringify(
{
"notification":{
"title":" FCM Starter message", //Any value
"body": "body of my msg", //Any value
"sound": "default", //If you want notification sound
"click_action": "FCM_PLUGIN_ACTIVITY", //Must be present for Android
"icon": "fcm_push_icon" //White icon Android resource
},
"data":{
"param1":"value1", //Any data to be retrieved in the notification callback
"param2": $scope.formData.message
},
"to":Globals.token, //Topic or single device
"priority":"high", //If not set, notification won't be delivered on completely closed iOS app
"restricted_package_name":"" //Optional. Set for application filtering
}
)
}).success(function(data){
alert("Success: " + JSON.stringify(data));
}).error(function(data){
alert("Error: " + JSON.stringify(data));
});
}