I'm building an Ionic app, installed the phonegap push plugin and set up the ionic io push settings with a real google developer api key. I didn't set any certificates or other keys for android in the ionic framework. For IOS I didn't set anything, as the app is still in development and there isn't a Apple Developer Account yet.
Now I was very happy, as I could receive push notifications. Now I found 2 problems with the notification:
1: It's single line, so the text would be "Hey this notification is too lo...". How can I make sure this is multi line?
2: When I press the notification, it doesn't open the app. Is this because it's a debug apk?
Here is the push data I send using the ionic push api:
tokens": tokens,
"profile": profile,
"notification": {
"title": title,
"message": message,
"android": {
"icon": "ic_stat_icon",
"icon_color": "#99004C"
},
"ios": {
"badge": "1"
}
}
After contacting the dev team of phonegap and ionic, we managed to find the solution. It turns out that there is a bug in the Ionic push framework OR the phonegap push plugin. Which one it is I'm not sure about.
It turns out that setting the android 'icon' in the data that you push to the Ionic push framework breaks the notification. This causes the notification to not be expendable, so only single line, not opening the app when you tap the notification, no sound etc.
The solution to this is setting the icon in the init of the phonegap plugin like so:
.run(function ($ionicPlatform, $http) {
$ionicPlatform.ready(function () {
var push = new Ionic.Push({
"debug": true,
"pluginConfig": {
"android": {
"icon": "ic_stat_icon"
}
}
});
push.register(function(token) {
console.log("Device token: " + token.token);
})
});
})
Related
I implemented Push Notifications in Xamarin Forms 5 following this article: https://learn.microsoft.com/en-us/azure/developer/mobile-apps/notification-hubs-backend-service-xamarin-forms but when targeting Android 31 on Pixel 6a I get non silent (data) notifications but when I tap on them with my app on the background I get an error saying "indirect notification activity start (trampoline) blocked for X".
The only workaround I've found is unchecking this options on:
Developer Settings -> Don't pop notifications on screen -> Don't pop notifications on screen:
Is there a way to disable this in Xamarin Forms code for Android so I don't have to ask my users to enable developer options and uncheck this option?
I found this article that explains why this restriction was added for Android 12 but haven't found a way to disable this option programmatically: https://proandroiddev.com/notification-trampoline-restrictions-android12-7d2a8b15bbe2
Update 2/7/2023:
I read the article that #Guangyu Bai - MSFT shared and I tried sending Android test notifications from my Azure Notification Hub with the following payload:
{
"notification": {
"title": "Notification Hub Test Notification",
"body": "This is a sample notification delivered by Azure Notification Hubs.",
"android": {
"actions": [
{
"title": "test",
"pressAction": {
"id": "default",
"launchActivity": "com.mypackage.test.MainActivity"
}
}
]
},
"data": {
"property1": "value1",
"property2": 42
}
}
}
I also tried this:
{
"notification": {
"title": "Notification Hub Test Notification",
"body": "This is a sample notification delivered by Azure Notification Hubs.",
"data": {
"property1": "value1",
"property2": 42
},
"android": {
"pressAction": {
"launchActivity": "com.mypackage.test.MainActivity",
"id": "default"
}
}
}
}
In both cases I got the notification on my simulator but when I tapped on it, my app did not appear and got same trampoline error. Am I missing something in my test payload?
I see many articles online where they use Notifee from React using pressAction to solve this problem in Android: https://notifee.app/react-native/reference/notificationpressaction but I haven't found how that translates to Firebase Messaging Service structure.
You can check this link Indirect notification activity start (trampoline) from blocked for more information.
Update the app
If your app starts an activity from a service or broadcast receiver that acts as a notification trampoline, complete the following migration steps:
Create a PendingIntent object associated with the activity that the
user sees after clicking the notification.
During the construction of the notification, use the PendingIntent object that you created in the previous step.
To identify the source of the activity, for example, to perform logging, use extra when posting notifications. For centralized logging, use ActivityLifecycleCallbacks or the Jetpack Lifecycle Watcher.
I'm trying to get push notifications running with React Native on iOS and Android.
To do so I am using firebase, as that seemed to be very convenient.
On Android I managed to show notifications when the app is in the foreground and at least show a log when the app is in the background, meaning I can process them.
On iOS I have not managed to do any of those, not from our custom backend in aws (via SNS), nor from firebase console with fcm token. What interestingly worked, was to show a badge on the ios app icon when sending broadcast messages to all users. So something seems to be received on the phone.
Now looking into this a little deeper I found that iOS needs notifications in the following format:
{
"Simulator Target Bundle": "com.compass.SomeExampleApp",
"aps": {
"badge": 0,
"alert": {
"title": "Push Notification Test",
"subtitle": "Hey! 👋",
"body": "Is this working?",
},
"sound":"default"
}
}
At least this is how you can test them on the sim.
1. Does it mean that I have to send the notifications in that same format? Or can it be something like:
{
"data":{},
"apns": {
"Simulator Target Bundle": "com.company.Example",
"aps": {
"alert": {
"body": "Wellcome to MyApp!",
"title": "MyApp"
}
}
}}
2. How can I send a format that iOS AND Android can read, show on screen and process in background if necessary?
Since we are sending only data messages I think this might be part of the issue, if I look at our backend.
Still when sending from firebase I am not able to show anything on screen on ios Device. Any hint?
I found a solution that actually works on both platforms although it is a bit weird since it seems to be outdated. Code needs to look like this to send to both iOS and Android:
{ "GCM": "{ \"notification\" : {\"content_available\" : true }, \"data\": { \"body\": \"Sample message for iOS endpoints\", \"title\":\"Hello world\"} }" }
As seen here APNS attributes seem to at least partly work if added within the GCM message like content_available in this case or badge.
THE SITUATION:
I use Ionic Push Notifications in my app.
With android everything works fine. But with IOS I don't receive any notification.
The code should be fine. The registration of the token it works properly also inside IOS devices. I can see the token in the database.
Also the cURL request code should be fine since it is working for android.
I will paste below all the info that may be useful:
THE CODE:
Initialization:
$ionicCloudProvider.init({
"core":
{
"app_id": "MY_APP_ID"
},
"push":
{
"sender_id": "MY_SENDER_ID",
"pluginConfig": {
"ios": {
"badge": true,
"sound": true
}
}
}
});
io-config.json:
{"app_id":"MY_APP_ID","api_key":"MY_API_KEY","dev_push":false,"gcm_key":"MY_GCM_KEY"}
THE CERTIFICATES:
In ionic.io I have setup the certificate - production mode - and activate all the credentials:
Inside the Apple Developer Console, the Push Notifications service is properly enabled (for production)
THE TESTS:
The app is already published in the App store.
Anyway i made tests both locally through XCODE (with the devices connected to it) and directly downloading the app from the app store.
I never received any notifications.
Making a test using POSTMAN this is the outcome:
{
"data": {
"status": "open",
"uuid": "b55a9024-d0d9-480e-a813-02053bcf2f2a",
"created": "2016-09-19T14:05:01.097422+00:00",
"state": "enqueued",
"app_id": "MY_APP_ID",
"config": {
"tokens": [
"THE_RECEIVER_TOKEN"
],
"notification": {
"message": "Push test"
},
"profile": "MY_IONIC_IO_PROFILE"
}
},
"meta": {
"status": 201,
"request_id": "dbb57cba-3817-42ee-baaf-7175b5f6c755",
"version": "2.0.0-beta.0"
}
}
THE QUESTION:
Why I am not receiving any notification on IOS?
Am i missing something?
Thank you!
Make sure notifications are enabled in your iOS settings app
Try using content_available = true in push payload
Try using priority = "high" in push payload
Make sure you're not sending using development certificates on a production app version or viceversa
Make sure your token is valid and refreshed
Docs about priority and content_available attributes
From docs:
On iOS, set content_available when the app server needs to send a Send-to-Sync message. An inactive client app will execute your logic in the background, while an app in the foreground will pass the message to didReceiveRemoteNotification:.
Make sure you have latest version of Ionic i,e v2 and above.
In path src/app/app.components.ts
After platform.ready().then(() => { add the below code
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);
});
After build (ionic build ios) open the ionic project in Xcode and in general and team section choose an account which has paid apple developer account.
Then in capabilities enable push notification.
If all the other constraints like certificate and .p12 files are done as per apple guild lines then push will surely work.
For more info visit this link : http://ionicframework.com/docs/v2/native/push/
I have a messaging app built using the Ionic framework (on cordova). I plan on building this for android, and I'd like a way to send and recieve push notifications from the app using javascript/ionic.
Are there any good tutorials out there on how to go about setting something like this up?
There is example application made available by Holly Schinsky. The core of it is the usage of PushPlugin which is the standard method to handle push notifications on Cordova. There is quite extensive tutorial provided for this subject on their documentation on that GitHub repository. The main method is pushNotification.register which registers the device to listen for push notifications.
If you instead need to trigger notification locally, you might want to take a look at Local notification plugin instead. With it you can add notifications to be shown on the device without the need for external services to send the push notifications.
Use this plugin https://github.com/phonegap-build/PushPlugin.
Android devices receive push notifications through the Google Cloud Messaging (GCM) service, whereas iOS devices receive them from the Apple Push Notifications (APN) Service.
The way the notifications are received (by sound, alert etc) is a combination of the options set in the application code upon registration as well as the user’s device settings for notifications.
If you want more specific follow below tutorial :
http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/
ngCordova has a plugin that supports Push Notifications. It has sample code for iOS and Android. Check it out: http://ngcordova.com/docs/plugins/pushNotifications/
The latest phonegap-plugin-push allows you to register and receive push notifications in your ionic apps. It is maintained at the following Github link:
https://github.com/phonegap/phonegap-plugin-push
Installation:
cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
Where the XXXXXXX in SENDER_ID="XXXXXXX" maps to the project number in the Google Developer Console. To find the project number login to the Google Developer Console, select your project and click the menu item in the screen shot below to display your project number.
If you are not creating an Android application you can put in anything for this value.
Note: You may need to specify the SENDER_ID variable in your package.json.
"cordovaPlugins": [
{
"variables": {
"SENDER_ID": "XXXXXXX"
},
"locator": "phonegap-plugin-push"
}
]
Note: You need to specify the SENDER_ID variable in your config.xml if you plan on installing/restoring plugins using the prepare method. The prepare method will skip installing the plugin otherwise.
<plugin name="phonegap-plugin-push" spec="1.6.0">
<param name="SENDER_ID" value="XXXXXXX" />
</plugin>
After installation you can now add code below to your main javascript file to register and receive push notifications:
$ionicPlatform.ready(function () {
var push = PushNotification.init({
android: {
senderID: "XXXXXXX"//, //project token number (12 digit) from https://console.developers.google.com
// forceShow: "true", //force show push notification when app is in foreground on Android only.
},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
},
ios: {
/*senderID: "XXXXXXX",*/ //If using GCM for ios, project token number (12 digit) from https://console.developers.google.com
/*gcmSandbox: 'true',*/ //If using GCM for ios
alert: 'true',
badge: 'true',
sound: 'true',
},
windows: {}
});
PushNotification.hasPermission(function (permissionResult) {
if (permissionResult.isEnabled) {
$log.debug("has permission for push notification");
/*Register device with GCM/APNs*/
push.on('registration', function (data) {
// data.registrationId
$log.debug("data.registrationId: " + data.registrationId);
});
push.on('notification', function (data) {
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
$log.debug(JSON.stringify(data));
});
push.on('error', function (e) {
// e.message
$log.debug("e.message: " + e.message);
//alert(e.message);
});
}
});
}
}
I have just started developing an app for Android and iPhone with trigger.io and parse.com. Now I'm stuck trying to add push notifications to the app. I send test pushes from the control panel at parse.com but nothing happens on the Android phone I'm testing with.
Here's what I've done:
Added the partners/parse section in config.json with applicationId and clientKey from the settings at parse.com
Added "event": true to the modules section in config.json
My config.json looks like this:
{
"config_version": "2",
"name": "My test app",
"author": "(my email)",
"version": "0.1",
"platform_version": "v1.4",
"description": "An empty app created by default",
"modules": {
"logging": {
"level": "DEBUG"
},
"event": true,
"geolocation": true,
"contact": true,
"file": true,
"is": true,
"media": true,
"notification": true,
"prefs": true,
"request": {
"permissions": ["http://*/*", "https://*/*"]
},
"tools": true,
"reload": true
},
"partners": {
"parse": {
"applicationId": "(my real app id)",
"clientKey": "(my real client key)"
}
}
}
Added the following code to listen for incoming pushes:
forge.logging.info('Adding push listener');
forge.event.messagePushed.addListener(function (msg) {
forge.logging.info('Got push: ');
forge.logging.info(JSON.stringify(msg));
}, function(err) {
forge.logging.error('There was an error receiving push!');
forge.logging.error(err);
});
To test the app I connect my Android phone by USB cable to my Mac and choose "Run android" in Trigger toolkit in the browser
In the log I see the messages "Initializing Parse and subscribing to default channel" and "Adding push listener"
I go to parse.com Push notifications and see that there is one connected device. I test to send several messages, some in "Message" mode, some in JSON mode.
Parse thinks that the messages get sent but nothing is logged on my side
I also tested connecting another Android phone. Parse then indicated that there were two reachable phones, but the new phone didn't get any messages either.
Looking at the documentation and examples I can't think of anything else to try. I cannot decide if this is a trigger.io problem or a parse.com problem.
Does anyone have any ideas?
Is this a problem just when you're using the default broadcast - rather than a named - channel?
We have a fix for a problem with the broadcast channel on Android ready to be deployed tomorrow or early next week - it will be in platform version v1.4.27.