ios開発にプッシュ通知を実装する方法

1.Push通知
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:15]; if (notification != nil) {
// push time
notification.fireDate = pushDate;
// set zone
notification.timeZone = [NSTimeZone defaultTimeZone];
// 時間間隔
notification.repeatInterval = kCFCalendarUnitDay;
// push sound
notification.soundName = UILocalNotificationDefaultSoundName;
// push content
notification.alertBody = @"arkgame.com provide it"; notification.applicationIconBadgeNumber = 1;
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
notification.userInfo = info;
//push add UIApplication
UIApplication *app = [UIApplication sharedApplication]; [app scheduleLocalNotification:notification];
}

2.プッシュ通知の取得
– (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
application.applicationIconBadgeNumber -= 1;
}

3.プッシュ通知の解除
// UIApplicationの取得
UIApplication *app = [UIApplication sharedApplication];
//プッシュの配列を取得
NSArray *localArray = [app scheduledLocalNotifications];
//通知オブジェクトの宣言
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"key value"]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//同じキーが既に存在するかどうか
if (!localNotification) {
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//プッシュの解除
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}

IOS

Posted by arkgame