Can someone help on how to get device token on ios using ionic only. I've managed to get device token using Xcode but is it possible to get device token using ionic only?
I need to to add the device token when the users log in the app
Try this
using this plugin
cordova plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
after that adding this
in to ionicPlatform.ready
var push = PushNotification.init({
"android": {
"senderID": "SENDER-ID"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event");
//here is your registration id
console.log(data.registrationId);
});
I may be very late in answering your question, your answer is as below
Update your ionic to latest version i,e v2 and above.
Then create a project and add this plugin
cordova plugin add phonegap-plugin-push --variable
SENDER_ID="XXXXXXXXX
Next goto src/app/app.component.ts file and add this code after platform.ready().then(()
var push = Push.init({
android: {
senderID: "XXXXXXXXX"
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
push.on('registration', (data) => {
console.log(data.registrationId);
alert(data.registrationId.toString());
});
push.on('notification', (data) => {
console.log(data);
alert("Hi, Am a push notification");
});
Perform all the certification part according to apple guidelines(if
any problem please do comment).
Finally implement push notification server side either in java or php or ionic.io ! All the best...
Related
In my Flutter app, when I send push notification with Postman, the data is coming to my Android test device,
{notification: {title: MugSafe Powerbank Sizi Bekliyor, body: Hemen alışverişe başlayın},
data: {priority: high, data1: helloworld, click_action: FLUTTER_NOTIFICATION_CLICK,
content_available: true}}
but it is not coming to Ios simulator or real device even logcat.
It works as necessary in Android but not Ios
Should I do something in Xcode for this issue?
My post;
{
"to":"devicetoken",
"notification": {
"body": "Hemen alışverişe başlayın",
"title": "MugSafe Powerbank Sizi Bekliyor",
"sound":"default",
"sound2":"default",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"data": {
"data1":"helloworld"
},
"content_available": true,
"priority": "high"
}
Here is a link where the setup for Cloud Messaging for iOS is explained (https://firebase.flutter.dev/docs/messaging/apple-integration). After you did everything mentioned there, it should work as expected!
Attention: Apple does not support notifications on their simulators!
I created a sample Cordova app which is using "phonegap-push-plugin".
That app doesn't have any complexity. On "deviceready" I run the plugin initialization code as shown here:
var push = PushNotification.init({android: {}, ios: {
sound: true,
alert: true,
badge: true,
categories: {
invite: {
yes: {
callback: 'accept',
title: 'Accept',
foreground: true,
destructive: false
},
no: {
callback: 'reject',
title: 'Reject',
foreground: true,
destructive: false
},
maybe: {
callback: 'maybe',
title: 'Maybe',
foreground: true,
destructive: false
}
},
delete: {
yes: {
callback: 'doDelete',
title: 'Delete',
foreground: true,
destructive: true
},
no: {
callback: 'cancel',
title: 'Cancel',
foreground: true,
destructive: false
}
}
}
}})
push.on('notification', data => {
console.log(data.message);
console.log(data.title);
console.log(data.count);
console.log(data.sound);
console.log(data.image);
console.log(data.additionalData);
})
push.on('emailGuests', data => {
console.log('I should email my guests');
});
push.on('snooze', data => {
console.log('Remind me later');
});
push.on('registration', data => {
console.log(data.registrationId);
console.log(data.registrationType);
});
push.subscribe('xx', console.log)
And this is the log output to console:
=> Successfully subscribe to topic xx
// The first run (after app install) will ask for permissions. If I click allow the lines below are printed to console.
=> dCAtjhCFBcU:APA91bG90c8VhNl_BzZ-2e9fmq_9fN6jfrRNJ1LPCRIpKnZ-AG-eLY4xtX84oJRZBh2D....KtNNQ35GM8ubPF5zr8HqeB6jffs
=> FCM
In order to push I'm sending the following payload to the Legacy Server https://fcm.googleapis.com/fcm/send.
{
"priority": "high",
"to": "/topics/xx", // I tried this but I also tried to specify the device token received upon "registration" event. I did this using to:<device_token> and also using registration_ids: [<device_token>].
"notification": {
"title": "My Message",
"body": "My Message Body",
"badge": 1,
"content-available": "1", // I tried with and without
"category": "identifier", // I tried with and without
"thread-id": "id", // I tried with and without
"sound": "default",
"icon": "default"
},
"data": {
"title": "A short string describing the purpose of the notification",
"body": "The text of the alert message",
"clubId": 1000
},
"notId": 1,
"custom_key1": "value1",
"custom_key2": "value2"
}
Note: I tried every combination possible in what concerns the app state: App in background; app closed; app in foreground; The event "notification" has never fired and the push notification was never received.
The request sent to the FCM server returns a message id when I use the topic (which is understandable since other devices subscribe the topic). For that reason my android that has subscribed to the same topic receives the message. The iOS in the other hand receives nothing!
{
"message_id": 5059997308576486332
}
If I try to specify the token that I received upon registration, I will get a slightly different message. Most of the time the token received upon registration works and the results will contain a string id. But this is temporary since a few minutes later the token become "NotRegistered".
{
"multicast_id": 88880398234xxxxx7,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered" // <-- This happens after a few minutes. I have to delete the app and reinstall it in order to get a new token.
}
]
}
This is the build configuration
Notifications are correctly enabled on my iOS device. What am I missing?
Updated:
Accessing Apple's APN directly (yup... no FCM!)
I would like to send my push notifications through FCM but in order to determine the cause of the issues described above, I decided to try APN directly. To do this, I had to remove the from the app's config.xml, so phonegap-push-plugin can obtain a token from APN and not from FCM.
Now, using the new token and a server that uses node-apn module to communicate with APN server, I'm able to send push notifications to my iOS app. The downside of this, is that I lose the ability to push to topics since this is a FCM only feature.
The only thing that I still don't know is how to use the topic to target devices in the APN network, that are subscribed by the push.subscribe() method.
Checkout my issue here.
Any help on this too?
So, turns out there's an issue with the push plugin.
This issue only impacts users on iOS device apps that use the FCM.
If you use the APNs it works.
You can check my answer in here: https://github.com/phonegap/phonegap-plugin-push/issues/2644#issuecomment-445346335
And my initial issue reporting in here:
https://github.com/phonegap/phonegap-plugin-push/issues/2613
I'm using the phonegap-plugin-push on my ionic3 app and I've been facing this issue with the iOS platform (android is ok). When the app is in background/foreground everything works as expected. When the app is not running however, I get the message on the notification tray but neither tapping the notification or the app icon triggers the on('notification') event, it simply runs and ignore the incoming notification.
I'm using the { ... content-available: 1} in the aps json message as suggested by the docs, but it doesn't seem to have any effect (though, it does have effect on Android), the ionic app itself never gets the message even though the device received it and displayed it on the tray.
Bellow, I show my configurations and a sample message sent to the push services. If anyone have any idea on how to sort this out will be appreciated, I've tested on ios 8.x and ios 11.x, both have presented the same behavior.
const options: PushOptions = {
android: {
senderID: '*************',
icon: 'ic_stat_ic_notification'
},
ios: {
alert: 'true',
badge: 'true',
sound: 'true'
},
windows: {}
};
platform.ready().then(() => {
push.on('registration').subscribe((data: EventResponse) => {
// register data.registrationId
});
push.on('notification').subscribe((data: EventResponse) => {
alert(data.message)
});
});
Sample message:
{
"aps": {
"alert": "This is a Push",
"badge": 1,
"content-available": 1
},
"payload": {
"type": "news"
}
}
This is too long of a comment. It's more of an indirect answer.
1) or the app icon triggers the on('notification') event <-- that's expected. Imagine your app just received 10 notifications. How should it know which notification you intended to open?
2) neither tapping the notification <-- this should trigger something in your code. Do you have didReceiveNotificationResponse implemented? Does that get called? Or does your didFinishLaunching get launched with a remoteNotification key? How about your application(_:didReceiveRemoteNotification:fetchCompletionHandler:) does that get called?
the case: I'm using iOS Enterprise Program for Ionic 2 app. One of issues for such distribution type was in supporting update flow. To solve this, we created simple JSON on server side to check available update. When update is available, user sees alert with suggestion to update app or exit.
app.component.ts
iosUpdateCheck() {
this.http.get(`${this.config.iosEnterpriseEndpoint}latest.json`).map(res =>
<{version: string}>res.json()).subscribe((res) => {
if (res.version !== this.config.appVersionNumber) {
this.message.alertCtrl.create({
title: 'Update is available!',
message: 'Please, update your terminal to proceed',
buttons: [
{
text: 'Update',
handler: () => {
console.log(this.TAG + 'iosUpdateCheck: update clicked');
//I tried just href also
window.window.open(`<a href=itms-services://?action=download-manifest&url=${this.config.iosEnterpriseEndpoint}manifest.plist />`,'_system');
}
},
{
text: 'Exit',
role: 'cancel',
handler: () => {
console.log(this.TAG + 'iosUpdateCheck: exit clicked');
this.platform.exitApp();
}
},
]
}).present();
}
});
}
I also added in config.xml <allow-intent href="itms-services:*"/> but still getting same error(sorry for screenshot, it's impossible to copy error link in Safari):
If I will use just a link it works fine but does not correspond to the objectives pursued.
I think this ionic-native plugin might help you: Ionic Native Market plugin.
Installation:
ionic cordova plugin add cordova-plugin-market
npm install --save #ionic-native/market
And don't for get to add it to your app.module.
Usage:
import { Market } from '#ionic-native/market';
constructor(private market: Market) { }
this.market.open('your.package.name');
I am having a hard time to make this work. What Im using:
1 - The Ionic Framework (version 1.7.14)
2 - Ionic push which is instantiated with: Ionic.Push. So Im not using ngcordova for push
3 -A real device, an Iphone 6s. And I am on OSX.
Since I began sending push notifications from Ionic Push
http://docs.ionic.io/docs/push-overview
something strange happened. I have sent a certain number of pushes both from curl and my own backend application.
The problem was that that badge with number "1" never disappeared from the app icon. I have tried to delete and reinstall the app using Iphone and xcode. But each time the app is installed, the badge with the number 1 is directly shown on the app icon. And even when I go inside the app and then close it, then the badge is still there. Why?
I didnt really understand who should take care of resetting the badge on the app icon. Is it the javascript code in Ionic?
SHouldn't it be managed by default by the OS? Shouldn't the OS clear the badge on the app icon as soon as you open the app that has badges?
I had this code in my js-controller:
var push = new Ionic.Push({
"debug": true,
"pluginConfig": {
"ios": {
"badge": false,
"sound": true
},
"android": {
"iconColor": "#343434"
}
}
});
However, it didnt help. The badge on the app icon is still there.
So as a last thing I did the following:
1 - I removed the code for push in javascript.
2 - I opened "settings" on my iphone and disabled my app to be updated in background.
No luck. The damned badge is still there in the right top corner of my app icon.
Try the following
Change "badge" : false to "badge" : 0
Because badge denotes the total count of notification and not boolean Value for show/hide.
Give it as 0
Hope it helps..
According to Ionic documentation on push, options specified in pluginConfig are passed to Phonegap Push Plugin.
If you look at the plugin documentation you'll see that clearBadge option is what you are looking for, this should be specified both for android and ios, like this:
"pluginConfig": {
"ios": {
"badge": true,
"sound": true,
"clearBadge": true
},
"android": {
"clearBadge": true
}
}
If you are using Phonegap plugin push, you can also do this.
var pushNotification = PushNotification.init(
{ "android": {"senderID": config.senderId, "icon": "notification","clearBadge":"true"},
"ios": {"alert": "true", "badge": "false", "sound": "true", "clearBadge": "true"},
"windows": {} } );
pushNotification.on('notification', function(data) {
pushNotification.setApplicationIconBadgeNumber(function() {
console.log("clear badge success");
}, function() {
console.log("clear badge error");
}, 0); //setting 0 will clear the badge
console.log("notification " + JSON.stringify(data));
});
You can see the doc here