-2

I am trying lot but not succeed yet to get silent notification when app is killed state

Here code I am trying ..

APS data:

{
  "aps": {
          "content-available": 1,
          "sound": ""
         }

}


    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSDictionary *userInfo1 = userInfo;
NSLog(@"userInfo: %@", userInfo1);

//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.

if (application.applicationState == UIApplicationStateActive)
{
    //opened from a push notification when the app was on background

    NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was Running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertView show];


}
else
{
    // a push notification when the app is running. So that you can display an alert and push in any view

    NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]);
      [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo];
}
}
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
SANTOSH
  • 183
  • 1
  • 15
  • Look at the doc (discussion part) of `application:didReceiveRemoteNotification:fetchCompletionHandler:` or even `application:didReceiveRemoteNotification:` that recommends to implement the other one. – Larme May 31 '17 at 09:17
  • but when {"aps":{"alert":"Enter your message","badge":1,"sound":"default"}} working fine even killed state too – SANTOSH May 31 '17 at 09:17
  • @larme can u please elaborate ..... – SANTOSH May 31 '17 at 09:19
  • " In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again." – Larme May 31 '17 at 09:23
  • I am trying lot but not succeed yet to get silent notification when app is killed state Dude, sorry to tell you this, but, silent notification do not work when app is killed, whatever you do will not work. – teixeiras May 31 '17 at 09:34
  • https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623013-application "However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again." – teixeiras May 31 '17 at 09:35
  • totally agreed with @teixeiras iOS push notification is totally different with android. In Android our broadcast receiver triggered no matter if app is in foreground or background. But in iOS if is in foreground than only the application:didReceiveRemoteNotification triggered. And if app is in background than if user tab on the notification from notification centre, than only application:didReceiveRemoteNotification: get triggered Please +1 if it's helpful – Prakhar Singh May 31 '17 at 11:14
  • @prakhar Let me clear Should we change/modify payload before displaying in notification area of ios device in regular remote notification not silent . – SANTOSH May 31 '17 at 11:53
  • @SANTOSH sorry buddy. But I don't think it is possible in iOS. Plus, I would suggest you not to play with native functionality of iOS. Apple app review department is very strict about native functionalities. They just need a small reason to reject your app. – Prakhar Singh May 31 '17 at 11:56

2 Answers2

2

In the payload’s aps dictionary must not contains the alert, sound, or badge keys.

{
    "aps":{
        "content-available" : 1
    }
}

Please try this.

  • I am trying with Slient notification @akshay.....then how can I add alert and badge keys ....... and tell you main reason to use this type of payload is to change my alert body before deliver to notification – SANTOSH May 31 '17 at 11:10
  • @SANTOSH As per apple documentation :- When a silent notification is delivered to the user’s device, iOS wakes up your app in the background and gives it up to 30 seconds to run. In iOS, the system delivers silent notifications by calling the application:didReceiveRemoteNotification:fetchCompletionHandler: method of your app delegate. Use that method to initiate any download operations needed to fetch new data. Processing remote notifications in the background requires that you add the appropriate background modes to your app. – Sumit singh May 31 '17 at 11:27
0

You should implement as in AppDelegate.m

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSDictionary *aps = [userInfo objectForKey:@"aps"];
    NSLog(@"hello");
    NSLog(@"userinfo---->>>>%@",userInfo);
    [UIApplication sharedApplication].applicationIconBadgeNumber=[[aps objectForKey:@"badge"] integerValue];
    [self application:application didReceiveRemoteNotification:userInfo];
}
Bilal
  • 18,478
  • 8
  • 57
  • 72
Nakul Sharma
  • 600
  • 1
  • 16
  • 23