-1

Hello friends I am using FCM first time in IOS, I have a condition. when I get notification I have to insert some data, But unable to do. Without sliding I am not able to trigger any function.So can anyone advice me how to resolve this issue. Only this function is calling after sliding

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

if (application.applicationState == UIApplicationStateActive){
    //application was in foreground
    NSLog(@"%@",userInfo);
} else if (application.applicationState == UIApplicationStateInactive){
    //application was in background
    NSLog(@"%@",userInfo);
}else if (application.applicationState == UIApplicationStateBackground){
     NSLog(@"%@",userInfo);
}


}

Can anyone help me,Thanks in advance.

Udai kumar
  • 233
  • 2
  • 17
  • is this function getting called at all? You can easily verify that with a breakpoint. Also in your payload, are you setting `content-available` to `1`? – mfaani Sep 06 '17 at 11:51
  • @Honey Thanks to reply, Yes I used content-available = true on server side,I also used breakpoint and after sliding notification only this method triggers. But I need a method that trigger when notification comes to Iphone – Udai kumar Sep 06 '17 at 11:54
  • This method gets triggered the moment your phone **receives** notification. So I don't understand what the problem is. If you want to do something **upon tapping** then you have to use: [`userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: `](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter?language=objc). For more see [here](https://stackoverflow.com/questions/44425744/alternative-to-usernotificationcenterdelegates-willpresent-when-app-is-in-backg/44705892#44705892) – mfaani Sep 06 '17 at 11:57
  • try this :-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { } – Kuldeep Sep 06 '17 at 12:00
  • @Honey Thanks but this method triggers when I slide the notification message. If I close/cancel it ,this method have not called. – Udai kumar Sep 06 '17 at 12:03
  • @Kuldeep that function [is deprecated in iOS 10](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc) and shouldn't be used. – mfaani Sep 06 '17 at 12:03
  • @Udaikumar 'this' means which function. Be specific – mfaani Sep 06 '17 at 12:04
  • @Kuldeep Thanks for replay bro, this function is not working. It is not triggering in any condition. – Udai kumar Sep 06 '17 at 12:04
  • @Honey -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ this – Udai kumar Sep 06 '17 at 12:05
  • did you get notifications? – Kuldeep Sep 06 '17 at 12:09
  • @Kuldeep yes I get notification but with -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ this method – Udai kumar Sep 06 '17 at 12:09
  • than you just have to get data from userInfo and based on that you just need to insert your data. – Kuldeep Sep 06 '17 at 12:16
  • @Kuldeep Yes I am getting data but while sliding or opening the notification. But I want a method to trigger when notification comes in Iphone like android a method called is "onMessageReceived" – Udai kumar Sep 06 '17 at 12:18

1 Answers1

0

You need to add UNNotificationCategoryOptionCustomDismissAction to the category you register with your notificationCenter & add that category to the content of your notification (because the 'registered category of notificationCenter' should match with the 'category of the notification's content it receives').

For more see this moment from the Introduction to Notifications WWDC 2016

EDIT: What are categories?

Notifications by themselves are just messages which contain a body and nothing more. If you want to add any form of action then you need to do 2 things:

  • add that action to the content of the notification
  • add that action to your usernotificationCenter.

categories help you add/group these actions and add accordingly. Sometimes a single notification my need to be dismissed, replied, liked <-- 3 different actions, grouped into 1 category.

To register a category with the unusernotificationCenter do:

UNNotificationCategory* generalCategory = [UNNotificationCategory
     categoryWithIdentifier:@"GENERAL"
     actions:@[]
     intentIdentifiers:@[]
     options:UNNotificationCategoryOptionCustomDismissAction];

// Register the notification categories.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObjects:generalCategory, nil]];

copied from: Registering the Notification Categories for Your App

And then from your server also add the same category name ie from the above example add generalCategory to your category key. For more see here.

And then from your userNotificationCenter:didReceiveNotificationResponse:withCo‌​mpletionHandler: you can do:

switch response.actionIdentifier and handle the tapping using the case of UNNotificationDismissActionIdentifier

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • I made an edit. Make sure you watch the WWDC video entirely. After that go through this [tutorial](https://code.tutsplus.com/tutorials/an-introduction-to-the-usernotifications-framework--cms-27250). Everything will be more straightforward after this. – mfaani Sep 06 '17 at 13:59
  • @Udaikumar did you go through the WWDC and tutorial? Did the answer help? – mfaani Sep 07 '17 at 16:03
  • Thanks for your reply, I am working on this. Bro I think these function help in APNs, but I am trying to using FCM that is google notification. Yet I am trying to do, your post is really very informative but unable to understand how to implement it. I am not able to add action in notification centre,how could i used it – Udai kumar Sep 08 '17 at 07:39
  • can you say one thing, for triggering ,I have to use this code to server side? For triggering method can i use notification centre code on serverside? – Udai kumar Sep 08 '17 at 11:36
  • And if you dont know how to trigger a notification then ask a new question. But beforeyou fo that make sure you search the available answers/questions. – mfaani Sep 08 '17 at 12:15