I want to create cheat for the tester to check the push notification. I have a working example from a web link and it's working. I hit the same web-link inside the app and received the success response but no notification. The Same thing is working from a web browser.
eg. I have API suppose www.example.com/send/notif/44 to send the notification to my iPhone 6 and I am calling it from my app dashboard screen notify button.
Please help.
You have to initialize 3 methods to receive remote notifications.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
<#code#>
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
<#code#>
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
<#code#>
}
If app is in foreground, it still receives push notification but not any banner is displayed by the OS. So, a tester should go to background immediately when they push that test button.
Related
I have set up the silent push notification for my app:
1. I configured the push notification from all places, i.e., XCode, Apple Developer portal with proper certificate
2. I enabled background capability
3. I included "content-available" in the json payload.
However, my App can not receive silent push sometimes.
To be more specific, neither
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
......
}
nor
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (_: UIBackgroundFetchResult) -> Void) {
......
}
has been invoked when the push message arrives.
How can I get the information in the push notification in such state?
I'm waiting online.
You question is twofold:
1. why the two delegate callbacks can not be invoked
2. what should you do to receive the data.
I did some hand test and here is the result:
1, when an app is in killed state, the two callback can not be invoked indeed
2. however, when you open the app next time,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
}
can be invoked and you can get the data from there.
I hope this is useful.
I have been battling with this for a while now but I've finally succumbed to submitting a question to see if anyone else has had an issue.
Essentially I'm trying to use the Facebook Analytics push service and only seem to be receiving push notifications some of the time. I've been intercepting the API calls using charles proxy to ensure that push tokens are being sent correctly by the Facebook SDK and events are being logged as expected. From this perspective everything is fine.
Next I use the 'Push campaign setup verification' on the settings page to try and send push and in-app push tests, and these also work fine (cards are displayed correctly on the devices etc.) on the 3 test devices I'm using.
The problem arrises when trying to create a campaign to send pushes to a certain segment of the devices. The first device I tried with worked with no issues at all, but then subsequent devices (with no changes to the codebase at all) wouldn't receive any pushes from any campaigns that I set up. My target audience is 'Device OS is iOS' so I would expect that all iOS devices would receive the push from the campaign. This seemed a bit odd, so I deleted the app off all the devices and rebuilt them again (ensuring that the test push worked on all devices) and setup the campaign again, but this time even the first device no longer worked. In the events debugger I get a 'Push Notification Error' and an error_message of 'InvalidDeviceOS' which makes no sense at all.
I've been going back and fourth with this for a few hours now with no success. I can see the tokens being sent to Facebook, and I can see the events being logged in the event debugger, I can use the push test service in settings and all devices receive pushes without an issue, but as soon as I try to use a campaign I get nothing.
For completion purposes here are the snippets of code I'm using:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// ...
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
// ...
}
func application(application: UIApplication, didRegisterUserNotificationSettings settings: UIUserNotificationSettings) {
UIApplication.sharedApplication().registerForRemoteNotifications()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FBSDKAppEvents.setPushNotificationsDeviceToken(deviceToken)
}
Finally here is the events debugger after opening a fresh install on 3 different devices (see the first launch events, 2 app launches have ID's and 1 doesn't as it's not logged in yet).
Any help with this would be great, thanks.
UPDATE: I created a brand new app and a brand new Facebook analytics instance, new push certificates etc. Initially I started receiving pushes from campaigns, but once I deleted and reinstalled the app the campaign messaging started to fail again. I then installed it onto a different device and cloned the existing campaign and tried again. This then resulted in the original device getting the push but the new device didn't... the mind boggles!
Now I don't know whats going on. On the face of it the Facebook service seems to get flaky once a push error comes back from APNS, but it's really difficult to prove that assumption since the error reporting in the Event Debugger is next to useless.
Again for completeness here is the code from the new app:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
// MARK: Notification Methods
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
FBSDKAppEvents.setPushNotificationsDeviceToken(deviceToken)
FBSDKAppEvents.setUserID(NSUUID().uuidString)
var token: String = ""
for i in 0 ..< deviceToken.count {
token += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
}
print("Token: \(token)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if FBNotificationsManager.shared().canPresentPushCard(fromRemoteNotificationPayload: userInfo) {
FBNotificationsManager.shared().presentPushCard(forRemoteNotificationPayload: userInfo, from: nil, completion: nil)
} else {
print("Unknown Payload")
}
completionHandler(.newData)
}
UPDATE 2: After hours of experimenting and digging we came across this bug report on Facebook's support pages which seems to confirm this is not an isolated issue: https://developers.facebook.com/bugs/118059088679219/
I have integrated push notification through GCM everything is working fine. But I am not getting notification message and sound. And the function didReceiveNotification: called in app delegate. And also not getting in background state.
Before making any comment or downvote consider following things.
I assume you have configured App Identifier in Developer portal, if not visit Apple Developer center
You have generated required provisional Profile & Certificate from Apple Developer Portal. If not visit App Distribution Guide
Make sure you have configured your bundle identifier correctly as defined in Apple Developer portal.
Following answer guides to configure APNS using your custom backend to send Push Notifications not for FireBase/GCM. To configure it using Firebase or GCM(As Firebase Cloud Messaging (FCM) is the new version of GCM) follow Google documentation
If all the above things are configured correctly then follow below steps:
Step 1: Register for APNS with Appropriate settings in didFinishLaunchingWithOptions inside AppDelegate file
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
return true
}
Step 2: Add delegate methods to handle success or failure for APNS registration by adding following delegate methods
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Convert binary Device Token to a String (and remove the <,> and white space charaters).
var deviceTokenStr = deviceToken.description.stringByReplacingOccurrencesOfString(">", withString: "", options: nil, range: nil)
deviceTokenStr = deviceTokenStr.stringByReplacingOccurrencesOfString("<", withString: "", options: nil, range: nil)
deviceTokenStr = deviceTokenStr.stringByReplacingOccurrencesOfString(" ", withString: "", options: nil, range: nil)
print(deviceTokenStr);
// *** Store device token in your backend server to send Push Notification ***
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error)
}
Step 3: Now you have configured your APNS on device end, You can fire Push Notification from your server/backend, When Push Notification is received following method will be called when your app is in Foreground. Implement it into AppDelegate.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}
To handle Push Notification while your application is in background (but not killed by removing from multitask) you need to take care of following things.
Make sure you have enabled Background Modes in Project Navigation->Targets->Capabilities->Turn on Background Modes and select Remote Notifications.
Now implement following method to handle Push Notification while in background. Make sure you handle UIBackgroundFetchResult properly.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}
Note: If func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) method is implemented func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) method will not be called.
Read more about APNS in Apple Documentation.
Usually, iOS apps can receive push notifications via APNS not GCM and could not get any data when app is in background state. If iOS app gets push notification via APNS and it is in background state, the push notifications just shown in notification center & top of the screen with app's icon. If you see the notification, there's no problem with the server.
And there's no data arrived when app is in the background state, you should make your server api for the notifications data when the app is back on foreground state.
I have a Swift 2 iOS8+ app where I need to make a request to fetch JSON data when my app receives a push notification.
When the user clicks on the notification the app will go and fetch the data but I really need the data to be fetched as soon as the notification is received. This looks to be possible, is this the case?
I've implemented:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)){
and checking the launch options in:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
I've also enabled:
background fetch
remote notifications
None of this seems to help. If this is possible I'd be grateful for any pointers/tutorials on this.
As of iOS 9, instead of a normal push, you can use silent push notifications. When your app receives a silent push, the user is not notified, but your app can perform actions based on this notification. Then, when your background actions is finished, you create a local notification for the user.
Check out this tutorial for info on how to use silent notifications:
https://www.raywenderlich.com/123862/push-notifications-tutorial
How do I handle a push remote notification when the app is not running. Currently, when remote notification comes in, and my app is in foreground or background, my code is correctly handling the remote notification. But when the app is not running, and remote notification come in, I see a remote banner on top of screen, and when I tapped on it, my app get launch and go away. It doesn't seem to handle the remote notification correctly. Is this how iOS behave, or is there a way to handle it. Thanks. This is currently my code in AppDelegate.swift.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
...
println("..... application didFinishLaunchingWithOptions()")
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
// there is a notification...do stuff...
println("dinFInishLaunchingWithOption().. calling didREceiveRemoteNotification")
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
println("didRegisterForRemoteNotificationsWithDeviceToken")
}
fun application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
println(error)
println(error.localizedDescription)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
println("..... application didReceiveRemoteNotification()")
}