didReceiveRemoteNotification not working in the background
Implementing didReceiveRemoteNotification
and didReceiveRemoteNotification:fetchCompletionHandler
is the correct way, but you also need to do the following:
Make sure to register for remote notifications, see documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Also make sure to edit Info.plist
and check the "Enable Background Modes" and "Remote notifications" check boxes:
Additionally, you need to add "content-available":1
to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here updated):
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app
So payload should at least look like this:
{
aps = {
"content-available" : 1,
sound : ""
};
}
didReceiveRemoteNotification Not Working when clicked on notification (background) but when working when app is Foreground
Hello I just got solution for my answer !!
I tried navigation to the other view in the usernotificationcenter extension method And it worked .
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Entry through the Notification")
let application = UIApplication.shared
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "stationListVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
if response.actionIdentifier == "remindLater" {
let newDate = Date(timeInterval: 900, since: Date())
scheduleNotification(at: newDate)
}
}
}
Thanks all for the HELP ;)
Related Topics
How to Set Image for Bar Button with Swift
Using Audiobufferlist with Swift
iOS 9 Safari: Changing an Element to Fixed Position While Scrolling Won't Paint Until Scroll Stops
iOS Simulator Games Run Very Slow (Low Fps)
iOS How to Detect When App Was Removed from Process
Uitableviewcell Show White Background and Cannot Be Modified on iOS7
How to Fix the Xcrun Unable to Find Simctl Error
Posting JSON Data Using Afnetworking 2.0
Change Text of "Return" Keyboard Button
How to Send Data Back by Popviewcontrolleranimated for Swift
Uicollectionview's Cell Disappearing
Swift Doesn't Convert Objective-C Nserror** to Throws
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
Using Scrollview Programmatically in Swift 3
Swift - Saving Highscore Using Nsuserdefaults
How to Programmatically Fake a Touch Event to a Uibutton