I have found from searching in the Internet: After iOS7, even if an app is killed, if a user clicks a notification associated with the app, the app will invoke this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
I am using an Adhoc provisioning profile, and app is in background and was not killed.
My code is below:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// JPush
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo]; // handle the notification
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"%@--userinfo", userInfo);
NSLog(@"%ld--applicationState", (long)[UIApplication sharedApplication].applicationState);
When I test, I get nothing in the device log.
Edit -1
My code for registe APNs in application:didFinishLaunchingWithOptions
:
My iphone os version is 10.2
// 7.push regist
if (IOS10) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"succeeded!");
}else {
NSLog(@"error:%@", error);
}
}];
} else if (IOS8_10){//iOS8-iOS10
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {//iOS8以下
[application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
Edit-2
Refer to Chandan's comment, if add content-available = 1
to aps
dictionary, we can wake up the app, but how to add the value to the aps
use java
:
Code is below:
public static void pushIos(List<String> registrationIds,Map<String,String> map){
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.registrationId(registrationIds))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(map.get("title"))
.incrBadge(1)
.setSound("happy.caf")
.addExtra("pushId", map.get("pushId"))
.addExtra("pushType",map.get("pushType"))
.addExtra("title", map.get("title"))
.build())
.build())
.setMessage(Message.content("123"))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
try {
PushResult result = jPushClient.sendPush(payload);
System.out.println(result);
}catch(APIConnectionException e){
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}