iOS8でのプッシュ(push)通知の実装プログラムコード
サンプルコード:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0″)
– (void)registerPushNotice:(NSDictionary *)launchOptions{
//プッシュ登録
if(iOS8)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}
if (launchOptions) {
if([self pushNotificationOpen])
{
//プッシュ開始
NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotificationKey) {
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
[self didReceiveRemoteNotification:pushNotificationKey];
}
}
else
{
//do nothing…
}
}
else {
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
}
}
//プッシュがオープンかどうかを判断
– (BOOL)pushNotificationOpen
{
if (iOS8)
{
UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
return (types & UIRemoteNotificationTypeAlert);
}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}