iOS push notification won't fire application function - ios

So my problem is that I want to log when the push notification service gets activated for my app. I know for that to happen this function gets called
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken
so I put my logging code in there. However the breakpoints basically state that the program never enters that function. I just recently added the logging code and the device im testing on already has that app activated for push notifications.
Therefore my question is, what can I do, I want to test if my logging code is working or not.

I believe what you are looking for is
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
This will go in your AppDelegate.m

- (void)unregisterForRemoteNotifications

Related

How to handle push notification when app is in background but not suspended

My app receiving push notification, and showing appropriate info message for that. However when I'm clicking to the message, application becomes active but application didFinishLaunchingWithOptions is not getting called which is right i think, since the application is not suspended and it just resigns active. The question is how i can make sure that user clicked to message when application becomes to foreground ?
I think what you are looking for is this app delegate method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
It will be called if your app is backgrounded, and the notification payload will be delivered in the userInfo dictionary. This contrasts with the situation when the app is launched from cold start, when this method does not get called, and instead you check in the launchOptions dictionary for the payload.
However the preferred way to do this since iOS7 is to use this:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
This method is called when a user taps on a notification, regardless of whether the app is launched from cold start or foregrounded from background. So even if you are not using the completionHandler, it provides a more consistent way of accessing the notification payload. If this method is present, the older one does not get called.
If I understand the question correctly, you are asking how to be sure that the app was brought into the foreground as the result of the user “clicking” i.e. acting on a push notification.
When the app is not running at all, you can use -application:didFinishLaunchingWithOptions: as you mention. The launchOptions dictionary contains the payload, etc. — I won’t describe this since you already know how this works.
When the app IS running however, that method is not going to be called. Instead, - application:didReceiveRemoteNotification: is called. BTW this is called if the app was already in the foreground OR if it was in the background and the user “clicked” on the push notification banner/alert to open the app. It will not be called otherwise: so I believe this is exactly what you’re looking for.
The userInfo dictionary provided by this method will contain the notifications data, similarly to -application:didFinishLaunchingWithOptions: for more ad-hoc processing. (Note: userInfo and launchOptions are semantically different, but hopefully this is obvious. :))

How to get a notification like the mail app on iOS

My understanding is that both local and remote notification in iOS will not be displayed if the application is in the foreground.
However if I open the mail app on my iPhone and check my email, then on some occasions i get a banner shown to tell me I have received emails. This occurs when the email app is in the foreground.
Does anyone know how this is done (preferably on Swift)? Or is this privilege only available for Apple written applications.
Any help would be appreciated.
You should implement - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo method in your AppDelegate.
If you want to handle them by inflating views or whathaveyou, you need to do it in that particular method.

How to bage Number in iOS app while app is closed

I am using badge notifications in my app and it works fine but I get the badge number by calling a method. So if method is called then the badge number gets increased but how to call that method while app is closed.
- (void)repeatedMethod {
SOWObject *object =[[SOWObject alloc]init];
[object getBadgeNumber:[self getDBPath]];
// I get badgeArray from above method
[UIApplication sharedApplication].applicationIconBadgeNumber=badgeArray.count;
}
is there any way we can call this method each day when date is changed and update badge number.
So far as I know, you can do this with 3 options:
Use Silent notification - for iOS7 and above only. a bit complicated since you need to enable Push and do back-end intergration
Use Background refresh - Create a timer + UIBackgroundTaskIdentifier. (not 100% sure)
Use Local/Push Notification - disadvantage using this, user knows of such notification is triggered.
implement this delegate method in your appdelegate:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
This method is fired whenever the OS finds any local notifications.(it doesn't matter whether your app is in the background or in the foreground).
For more information & code look here
Look at the accepted answer.
Edited:
Answer for you to call a method while app is in background is here
So, it basically says not all the apps have access to background execution. Officially it's mentioned in the apple's developer site also: HERE

When do I schedule local notification

When the user leaves my iOS app, I want to schedule a local notification to remind him about my app. What is a correct place to do it? I cannot choose between
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidEnterBackground:(UIApplication *)application
Or there's no difference in the case?
I also have a second question. When my app launches (either if user pressed notification, or from launchpad), I should obviously remove all that reminding notifications. I guess I should do it in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
or maybe another method? Or it makes no difference again?
Do it all in applicationWillEnterForeground. Remove any old notifications that don't matter now the user it engaged with the app and install any new notifications for after this use session. If the notification fires when the user is still using the app then you don't need to display anything (and the system won't display anything either).

Local notification handling when application is closed

I have a question related to handling notifications but in case of application is closed. In my iOS application I schedule the local notifications, and I successfully handle them when application is either in foreground or background. But when application is closed and I get local notification I can't handle it. I mean after pressing "show" button I have to move the user to a specific window. But I don't know where to put the part of the code which deals with handling.
Any idea???
Thanks in advance.
Armen
Check - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in your app delegate, you should receive dictionary from your notification as launching options, as far as I know.
Try this
I could post some code hear but i think the better way is no copy/paste work. So here is the link after they show single notification, they explain how to handle notifications. tutorial about handling notifications

Resources