Yogendra Patel
Yogendra Patel

Reputation: 833

How handle silent push notification when application is InActive state in ios 9?

I have implement silent push notification.So "didReceiveRemoteNotification" method called when application is inactive state in ios 9.

There are some case when application is inactive state.

1.When user tab on particular notification.

2.When call or message receive.

3.When notification center and control center open.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 {
     if(application.applicationState == UIApplicationStateInactive)  //Inactive state
     {
         [self RedirectScreenBasedOnNotification:self.userInfoDic];//Screen Redirection code 
     }
 }

So how can i handle silent notification when app is inactive state?

I have face problem is when notification center open at that time if any notification come then redirection will do,but i want to stop that.

Notification payload:-

aps =     {
    alert = "Test Dev 5 startd following you ";
    "content-available" = 1;
    "link_url" = "https://raywenderlich.com";
    message =         {
        friend =             {
            email = "[email protected]";
            name = "Test Dev 5";
            photo = "";
            "user_id" = 27;
        };
        id = 3;
        "is_business_sent" = 0;
        message = "Test Dev 5 startd following you ";
    };
    sound = default;
}

Thanks in advance

Upvotes: 0

Views: 1899

Answers (2)

quellish
quellish

Reputation: 21254

Silent push notifications do not trigger user interactions. When a silent notification payload includes keys for user interaction things go wrong - iOS can't reason about wether the intent is to present something to the user, or to keep the notification silent and handled without user interaction. Sometimes the silent notification may work, other times it may be presented like a normal notification with user interaction. It can be one or the other, not both.

If the silent push key content-available is present in the aps payload the keys alert, sound, or badge should not be.

You can use my Push Notification Payload Validation Tool to check the content of your notification. The payload you posted in your question has several problems - the aps key should only contain Apple keys defined in Generating Push Notifications. All of your custom keys and values should be outside the aps object.

application:didReceiveRemoteNotification:fetchCompletionHandler: will only be called for silent push notifications. If the notification payload contains both content-available and one or more of alert, sound, or badge iOS will not know which method to call and you may see inconsistent behavior.

If you are just trying to show a non-silent notification you do not need to implement application:didReceiveRemoteNotification:fetchCompletionHandler:. Instead implement application:didReceiveRemoteNotification: for iOS 9 and userNotificationCenter:willPresentNotification:withCompletionHandler: for iOS 10 and later.

As far as silent notifications and the inactive application state, there is nothing special to be done here. Silent notifications are intended to 'hint' to the application that it should refresh content. When a silent notification is received the application is required to process the content update within 30 seconds and then call the fetch completion handler. When iOS executes the fetch completion handler it take a new restoration snapshot of the updated UI. This happens even when the application is inactive.

Upvotes: 2

manishsharma93
manishsharma93

Reputation: 1049

You can add your code in this If condition.

if (UIApplication.sharedApplication.applicationState != UIApplicationStateInactive) {
   //Write your code her, this will get executed when your app is not in Inactive state.
}

Upvotes: 0

Related Questions