3

I am searching the way about how to handle push notification payload data as soon as the notification reaches to the client app without opening or tapping it.And I am still not getting the data unless the user tap or open it from notification center or banner or alert.The function didReceiveRemoteNotification only triggered when the user click the notification on the screen.So,how to get the notification payload data when the notification arrive to client app even the user ignore(without open or tap) it.

INFO : I heard that GCM(Google Cloud Messaging) can make notification handler if the client app user tapped the notification or not.It can catch the notification payload json data as soon as it reach the client app without even need user to tap or open it.Is that right?

I really need a hand to pick me up with getting notification payload data on ios without even need a user to open or tap it.

Update : The app is still running on device which mean it was active.I can get the payload data when i click my notification which was "{aps:} json i get it.But,I still cant get the data when i don't open the notification"

Here is my state

When the app was at foreground,I get the data. 1.I run the App,

2.Send Notification,

3.Get the notification with an alert,

4.I get the data(payload).

Work fine when app is active.

But,when the app reach to background

1.Run The app,

2.Close The App by pressing home button,

3.Send Notification,

4.Get the notification.

5.But,cant get the data until i click notification that I was receive at banner or notification center.

But,when i click the notification at banner or notification it went to app and then i get the data.

Is there anyway that i can get the data if the app in background when the notification received.

Here is the code :

import UIKit
import RealmSwift

let realm = try! Realm()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var data : [NSObject : AnyObject]!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    //one singnal is the push notification service that i use for push notification.
    let oneSignal = OneSignal(launchOptions: launchOptions, appId: "__app_id__") { (message, additionalData, isActive) in
        NSLog("OneSignal Notification opened:\nMessage: %@", message)

        if additionalData != nil {
            NSLog("additionalData: %@", additionalData)
        }
    }
    oneSignal.enableInAppAlertNotification(true)
    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("User Info : \(userInfo)")
    if let custom = userInfo["custom"] as? NSDictionary{
        if let a = custom["a"] as? NSDictionary{
            print("A : \(a)")
        }
    }
}
tilo
  • 14,009
  • 6
  • 68
  • 85
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
  • do get notification data in this method didReceiveRemoteNotification ? – Maulik shah Dec 02 '15 at 07:54
  • @Maulik When user click(open) it,yes...I get the payload (json)data. – Thiha Aung Dec 02 '15 at 07:57
  • i think you don't open notification screen when app close am i right ? – Maulik shah Dec 02 '15 at 08:19
  • @Maulik,I really appreciate your help but,would you mind read my question again for more clearly.All I want to know was how to handle payload data of notification without user tapping or opening it.I updated the question,please take a look – Thiha Aung Dec 02 '15 at 08:29
  • if user not tap on notification where you want to get data..any view controller to get data – Maulik shah Dec 02 '15 at 08:45
  • without tapping you don't get any payload data – Maulik shah Dec 02 '15 at 08:47
  • Is there any way that i can catch the event and get the data when notification arrive? – Thiha Aung Dec 02 '15 at 08:51
  • Check documentation for [application:didReceiveRemoteNotification:userInfo: fetchCompletionHandler:handler](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:) – VChemezov Dec 02 '15 at 08:52
  • any view controller to get data? – Maulik shah Dec 02 '15 at 09:06
  • Guys,I want to get payload data "[aps:{xxxxx}]" when the notification arrive on device not letting the user don't tap or open from notification center or alert or banner.I know how to read the data or how to get the data using didReceivedRemoteNotification:userInfo:fetchxxx.But,I can only get it if the user click the notification when it arrive.If the user ignore it,I cant get it.I need help with that.ok? – Thiha Aung Dec 02 '15 at 09:10
  • please share your code – Maulik shah Dec 02 '15 at 09:38
  • I do have share the code.Please take a look. – Thiha Aung Dec 02 '15 at 10:36
  • When your app is in background or closed, you cannot get the data unless the notification is tapped in the notification centre. This in not possible in the current iOS SDK. – Karlos Dec 02 '15 at 12:23
  • I was wondering the same. I found [this post](http://stackoverflow.com/questions/22090989/background-task-when-apns-received) that might answer your question. – Quentin Malgaud Dec 02 '15 at 17:40
  • @QuentinMalgaud,did u solve it?If u can,welcome to post here m8 :) – Thiha Aung Dec 03 '15 at 03:47
  • Did you solve this? – user2722667 Nov 22 '16 at 10:47
  • There is no way,you can get the data without tapping the notification if your app is killed(no where in memory) – Thiha Aung Nov 22 '16 at 10:49

1 Answers1

1

I came across the same problem. As mentioned in the previous comments, this post is quite helpful.

According to Apple,

When a remote notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Launching your app in the background gives you time to process the notification and download any data associated with it, minimizing the amount of time that elapses between the arrival of the notification and displaying that data to the user.

The first thing you have to do is to allow your app to do something when in background. You do this by adding Required background mode in your info.plist, then add it App downloads content in response to push notifications. Your info.plist should look something like this:

info.plist Required background modes

Now this is done, your app should awake when it receive a notification. You can execute a small code inside didReceiveRemoteNotification. Such as this.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
//do some code here
UIBackgroundFetchResult.NewData
}

Note that you have to pay attention to the completionHandler:

As soon as you finish processing the notification, you must call the block in the handler parameter or your app will be terminated. Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block.

Let me know if everything is clear :)

Community
  • 1
  • 1
Quentin Malgaud
  • 405
  • 6
  • 21
  • how u handle payload data in the given delegate, i mean my payload data is a son and whole son is showing in notification tray i just want to show the message from it – veeresh kumbar Aug 07 '18 at 07:42