Phonegap Plugin Push notification stays in tray when clicked on iOS - ios

I built a phonegap application which receives push notifications. When I run the application on Android, everything works perfectly, but when I run it on iOS, the push notifications stay in the notification tray after clicking them.
I am using Phonegap Push Plugin for the Phonegap application and I'm using PushSharp for sending the notification. This is my code:
var push = PushNotification.init({
"android": { "senderID": "XXXXXXXX", "icon":"pushicon" },
"ios": { "alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
var deviceInfo = {
notificationId: data.registrationId,
phonePlatform: device.platform,
phoneVersion: device.version,
phoneModel: device.model
}
info = $.extend(info, deviceInfo);
localStorageService.setDeviceInfo(info);
});
push.on('notification', function (data) {
alert(data.additionalData.title);
});
push.on('error', function(e) {
console.log('Something went wrong: ' + e.message);
});
I've also tried calling push.finish(), but it makes no difference.
Does anybody know how I can make sure a notification on the notification tray dissapears after clicking on it?
Specs
Device: iPod Touch with iOS 7.0.4
Plugin: Phonegap plugin push

On iOS 8 and greater, when you tap the push it will be deleted from notification center, but on iOS 7 and older it doesn't work that way.
If you want to delete the push notification from notification center you'll have to set the badge to 0.
push.setApplicationIconBadgeNumber(function() {
console.log('success');
}, function() {
console.log('error');
}, 0);
If it doesn't work you can try to set it to a greater number first and then to 0
push.setApplicationIconBadgeNumber(function() {
push.setApplicationIconBadgeNumber(function() {
console.log('success');
}, function() {
console.log('error');
}, 0);
}, function() {
console.log('error');
}, 2);
Latest version of the plugin also has a clearAllNotifications method:
push.clearAllNotifications(function() {
console.log('success');
}, function() {
console.log('error');
});
Or you can also pass the clearBadge option to the init method
var push = PushNotification.init({
"android": { "senderID": "XXXXXXXX", "icon":"pushicon" },
"ios": { "alert": "true", "badge": "true", "sound": "true", clearBadge: "true" },
"windows": {}
});

Related

Reactnative IOS pushotification/PushNotificationIOS onNotification does not called directly when notification received

I used reactnative PushNotification/PushNotificationIOS modules , My issue is when notification received on foreground to my device IOS ,the function onNotification: function(notification) {} does not called directly is called by click only .
I want to fetch data notification paylaod sent via pububnub console directly without any user interaction ,this function is working fine in android but ios not all.
the console does not show anything can help me to solve this issue and thanks.
This the function and imports module:
import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "#react-native-community/push-notification-ios";
onNotification: function(notification) {
if (Platform.OS=='ios')
{
console.log('notif',notification)
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
onAction: function (notification) {
},
onRegistrationError: function(err) {
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
senderID: FIREBASE_SENDER_ID,
})
}
and this the object pubnub to send :
{"pn_apns":{
"aps":{
"alert": {
"body": "Course disponible",
"title": "My course"
},
"sound": "beep.wav",
"data": { "reference": "ND1004332", "startstation": "" }
},
"pn_push":[
{
"push_type":"alert",
"auth_method":"token",
"targets":[
{
"environment":"development",
"topic":"com.test.fr"
}
],
"version":"v2"
}
]
}
}
import {Platform} from 'react-native';
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from "#react-native-community/push-notification-ios";
if (Platform.OS === 'ios') {
// Must be outside of any component LifeCycle (such as `componentDidMount`).
PushNotification.configure({
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
const { foreground, userInteraction, title, message } = notification;
if (foreground && (title || message) && !userInteraction) PushNotification.localNotification(notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
// alert: true,
// badge: true,
sound: true
},
});
}

how to add custom sound in iOS with flutter?

I am new to push notifications and I have this task to add a custom sound whenever the user receives a notification. I am not using flutter local notifications, just the normal firebase configurations and its working well. Can anyone help me add a custom sound for iOS. Any help would be greatly appreciated!
Here is the payload used and the firebase configurations.
options = {
"notification": {
"title": formatter.title,
"body": formatter.body,
"sound": "notification.caf",
"content_available": true,
},
"priority": "high",
"data": {
"badge": user.unopened_notification_count,
"body": formatter.body,
"title": formatter.title,
"id": notification.id,
"notifiable_type": notification.notifiable_type,
"notifiable_id": notification.notifiable_id,
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}
also here is my firebase configuration:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (Firebase.apps.isEmpty) await Firebase.initializeApp();
print('Handling a background message ${message.data['id']}');
}
handleNotifications() async {
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(badge: false, alert: false, sound: true); //presentation options for Apple notifications when received in the foreground.
FirebaseMessaging.onMessage.listen((message) async {
print('Got a message whilst in the FOREGROUND!');
return;
}).onData((data) async {
print('Got a DATA message whilst in the FOREGROUND!');
print('data from stream: ${data.data['id']}');
});
FirebaseMessaging.onMessageOpenedApp.listen((message) async {
print('NOTIFICATION MESSAGE TAPPED');
return;
}).onData((data) async {
print('NOTIFICATION MESSAGE TAPPED');
print('data from stream: ${data.data}');
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.instance.getInitialMessage().then((value) => value != null ? _firebaseMessagingBackgroundHandler : false);
return;
}
attached image here is where I placed the sound file inside Xcode

Phonegap, IOS , Device doesn't receive notification

I am developing a Phonegap application for iOS and would like to enable push notifications. I use GCM and when I send notification google return me success but IOS device doesn't show anything .
There is my code:
"android": {
"senderID": "xxxxxxxx", "icon": "icon", "badge": "true", "iconColor": "white", "sound": "true", "soundname": "nb"
},
"ios": {
"senderID": "xxxxxxxx",
"alert": "true",
"badge": "true",
"sound": "true",
"usesGCM":true,
"sandbox":false,
},
"windows": {}
.
First of all install the plugin of push notification.
phonegap plugin add phonegap-plugin-push
After the write the java script code as below for iOS push
var note = new apn.Notification();
note.badge = 1;
note.sound = "beep.wav"; //path to your sound
note.contentAvailable = 1;
note.alert = { "body" : msg, "action-loc-key" : "Show Me!" , "launch-image" : "mysplash.png"};
//note.payload = {'uuid': data.uuid}; // additional payload
note.device = data.deviceId;
var callback = function(errorNum, notification){
console.log('Error is: %s', errorNum);
//console.log("Note " + JSON.stringify(notification));
}
var options = {
gateway: 'gateway.sandbox.push.apple.com',
errorCallback: callback,
cert: 'test.pem',
key: 'test.pem', // ** NEED TO SET TO YOURS
passphrase: '', // ** NEED TO SET TO YOURS
//port: 2195,
enhanced: true,
cacheLength: 100
}
var apnProvider = new apn.Provider(options);
apnProvider.send(note, myDevice).then( (result) => {
console.log("Result :: ",JSON.stringify(result));
});
It will works.

Unable get message on IOS from firebase console with phonegap plugin

i have successfully implemented Firebase cloud messaging in my phonegap app with phonegap-plugin-push on IOS, i am receiving token from Firebase on register event, i print it in alert message and it shows me token.
Below is my code
function onDeviceReady(){
var push = PushNotification.init({
"android": {
"senderID": "29271112133",
sound: true,
vibrate: true
},
"ios": {"senderID": "29271112133","alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data)
{
globalpushtoken1 = data.registrationId;
alert("Token IS HERE.. "+globalpushtoken1);
checktoken(data.registrationId);
});
push.on('notification', function(data) {
if(data.additionalData.coldstart){
if(data.title=="New Offer !"){
window.location.replace("#offerspage"); }
}
});
push.on('error', function(e) {
alert("Push Error");
console.log("push error");
});
Now the problem is when i try to send some test message from firebase console and select User segment and select my app and send message it shows message status as completed however i dont get any notification on real device, and if i send the message to specific device using token of that device then it shows status as
FAILED Unregistered registration token " I am new to firebase and phonegap, please help me resolve this.
firebase console after sending message manually

GCM registrationId is not received on phonegap push plugin

Couple days ago I had an issue with getting APNS token instead of GCM token while using phonegap push plugin.
Well, I changed setup, I've put senderID in [ios] block, recompiled the app. Now I dont get ANY regustrationId at all on Iphone. It still works fine on Android. Can anyone tell me what can be a problem?
Here is setup of plugin:
var push = PushNotification.init({
android: {
senderID: "8225....8910"
},
ios: {
senderID: "8225....8910",
alert: "true",
badge: "true",
sound: "false"
},
windows: {}
});
And this event is never being called:
push.on('registration', function(data) {
$.ajax({
url: '/authentication/ajax-register-gcm-token/',
data: {token: data.registrationId},
success: function (json) {
alert('Phone registered' + data.registrationId);
}
});
});
The code you have used to initialize Push Notification Plugin Object is wrong. It should be as below:
var push = PushNotification.init({
android: {
senderID: "XXXXXXXXXXXX",
},
ios: {
alert: "true",
badge: "true",
sound: "true",
}
});
push.on('registration', function(data) {
console.log(data.registrationId);
registerDeviceToken(data.registrationId);
});
push.on('notification', function(data) {
console.log("notification event");
alert(JSON.stringify(data));
});
push.on('error', function(e) {
console.log("push error");
alert(JSON.stringify(e));
});
function registerDeviceToken(deviceToken){
//Register the registrationId or deviceToken to your server as per the webservice type and parameters configuration set
//From your code snippet above
$.ajax({
url: '/authentication/ajax-register-gcm-token/',
data: {token: deviceToken},
success: function (json) {
alert('Phone registered' + deviceToken);
}
});
}
There is no SenderID mentioned here in official link as well. Make sure you have turned on Push Notification service under Capabilities section of your Project and you have put correct development and production APNS p12 files and their respective passwords at the server side code. So if you are running App with Development Profile then there should be development p12 file environment sending Push Notification in order to be received on your iOS device. To set up Development and Production APNS p12 certificate, refer this link: APNS setup

Resources