APNS notification does not work when app is not running or background - iphone

I'm using pyapns to send notification to iPhone.
receiving notification when the app is running was success.
but, when app is not running or app is on background, it can't receive notification.
is it related with URL identifier or scheme? if not, what is the problem..?

Did you get called in
- (void)application:didReceiveRemoteNotification:
while you were in the app?

the problem from notification msg.
among the notify function args, there is notifications list arg.
note that, when you make this parameter, a dictionary of this list should contain below. so that the application can do alert reaction.
thePayLoad = {
'aps': {
'alert':'this option should be contained if you want to see the alert msg',
'sound':'k1DiveAlarm.caf',
'badge':42
},
'test_data': { 'foo': 'bar' }
}
In my case, I just sent msg not containing above. and in didReceiveRemoteNotification function, I made alert msg. but msg not containing alert information. so the device couldn't react.

Related

(Ionic) handleNotificationReceived does not get called if the app is killed

I am working on an ionic app and I am using onesignal for push notifications.
The problem I am facing is that the handleNotifiactionRecieved() does not get triggered upon receiving a push notification when the app is not killed (that is removed from the recent apps). Though it works as expected when the app's running but in the background(not inFocus).
setup code is something like this:
if (this.platform.is('cordova')) {
this.oneSignal.startInit('APP_ID');
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
this.openOneSignalMessage();
this.oneSignal.endInit();
}
my code is something like this:
openOneSignalMessage() {
this.oneSignal.handleNotificationReceived().subscribe((data) => {
this.MsgBody = data.payload.body
console.log('newMessageOneSignal MsgBody', this.MsgBody);
});
}
actual result: handleNotificationReceived() doesn't get called when the app is not running, that is killed.
expected result: handleNotificationReceived() should be called every time a push notification arrives even when the app is not open.
How do I trigger the method every time?
Thanks in advance.
Thanks for pointing out the issue in the docs. They have been updated with the correct info:
handleNotificationReceived (builder method)
Sets a notification received handler. Only called if the app is running in the foreground at the time the notification was received.
If you want to handle from a killed state, make sure to use the handleNotificationOpened method
I find this in the doc:
Sets a notification received handler. Only called if the app is running in the foreground or background at the time the notification was received.
Maybe your app is not working on background.
Or the problem is you need to call ur function after the startinit :
this.onesignal.startInit("YOUR_APPID")
this.onesignal.handleNotificationReceived()

Background notifications using Ionic like a mortal

I have been trying for several months to implement an app that receives notifications whose content I want to be stored in the database of my application. I am using the Firebase Messagging plugin and I am receiving the notifications correctly, both in Foreground and in Background.
However, in the background I am unable to execute my callback without the need to press the notification explicitly by the user. Likewise, there is no way to increase the badge of the application when it is not open.
Now, I do not understand how applications like WhatsApp, Telegram and any other application that is not made by mere mortals work. How is it possible that they can get the data in backgroud, manage the badges, synchronize messages, etc? Being that, supposedly the services like Firebase are limited in background. Even with plugins like Background Mode my application is suspended by Android when the user closes it.
The code that I am currently using to handle notifications is the following:
// In foreground (WORKS)
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
// Insert in DB
...
});
// In background (DOESN'T WORK)
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
// Insert in DB
...
});
What alternative is left? The only thing that occurs to me is to use notifications in Foreground and background only as a warning. So every time I open the application I'll have to call a message synchronization callback with the server (with badge management included).
If someone has some better way, I would greatly appreciate it if you throw a little light on the subject.
From already thank you very much
Ok, after more than a year I think it's time to close this question. I solved the problem this way:
Suppose I need to get a list of messages. This action is made by a function called getMessages()
When a new message is created in the backend, I send a push notification through Firebase service.
If the push notification is received in foreground I refresh call the method directly:
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background and the user taps on it there isn't a problem as it's supported by the plugin:
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background but the user DOES NOT tap on it and opens the app in another way than the notification, we need to excecute the corresponing function when the app is opened in app.component.ts file:
this.platform.ready().then(() => {
getMessages();
// Extra stuff like this.statusBar.styleDefault(); or this.splashScreen.hide();
});
That way all the cases are considered! You should keep in mind that apps like Whatsapp or Telegram are developed in official technologies like Java or Kotlin which not only have official native plugins, also its code are excecuted natively and not in a limited browser as Cordova or Capacitor frameworks do

Which app delegate function to send notifications in background?

I have searched this topic for a while but not found anything that does exactly what I want. I have this code that checks for when a new post is added to the database, and when that happens, I want to send the user a notification. Here is that code:
Database.database().reference().child("\(mySchool!)/posts").observe(.childAdded, with: { (snapshot) in
self.notifyNewPost()
})
My question is, which (if one) function should I put this inside of the app delegate to have this run in the background? Thanks!

iPhone push notifications information

Is it possible to transfer any information in the push notification beside "badge" "sound" and "text"?
For example, in the app "whatsapp" when a push notification appears and pressed, the app is opened but not going to the conversation. My guess is that it can't know what conversation to go to. But then I saw that in facebook messenger app it actually goes in the conversation. how does the messenger app know that what conversation to go to?
Also, if it is possible to transfer information, why is it that apps like whatsapp don't use it and also ask you for your name so it will be displayed in the push?
Yes, indeed. But your message size (in bytes) must not pass a certain threshold Apple has imposed. You can then pull that info in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions using something like this:
NSDictionary* dictionary = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
Where dictionary contains your push notification info.
There are apps that have permission to run in the background and other apps that have not. May be facebook messenger app have this permission and can receive push notifications and do whatever is needed to go to the correct conversation or user. I don't know if this is true but it could be a possible reason.
in this method we can display pushnotifications alerts and their action according to our app
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"did receive remore notification %#",userInfo);
if(isForground)
{
}
}
You should take a look at this documentation section Examples of JSON Payloads
At the bottom you can see custom payload examples like:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
Where acme1 and acme2 are custom data that you can pass to the push notification and get it inside you app after launched.
The data is available through UIApplicationDelegate callbacks as described here Handling Local and Remote Notifications
You can add more arguments in your payload. In our app we add something like
groupID, or type.
See this stack overflow for adding more payload arguments
APNS JSON PAYLOAD - more arguments
Make sure the message size doesnt exceed 256 bytes.It is the threshold limit for the payload

Is it possible to send a "silent" push notification on iPhone (without any alert to the user)?

I need to do it on a chat app to notice the client that there is new data to fetch from the server.
I know that technically it is possible with an empty payload (at least on ios 5 device it worked), but can Apple see it as abuse of service or is it OK to do it?
And is it technically possible on ios 4 device?
Yes, increment only badge number, dont send sound and alert, So, there will be one badge on the App icon.
So, here unknowingly the badge number will be incremented to show that something has been recieved when user watches the app icon.
But your device won't recive alert or empty payload and won't recieve sound also because you asked for “silent” push notification on iPhone
and while configuring in AppDelegate, configure only badges
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
Hope this will be helpful for you
You need to build the payload like this:
String payload =
'{
"aps":{
"content-available": 1
},
"data_id": 5,
"additional_data": ["some", "data"]
}'
In aps put the apple variales needed to make the push notification display a text, sound, badge... or just content-available to make it silent. Then, out of 'aps{...}' and separated with ',' put your custom fields to send the needed information for the download.
If you don't want the badge number to increase you could also try sending a sound notification which plays a silent sound file. (Unfortunately, I don't know if this is ok for Apple).