あなたは次のようにして、あなたのiOSアプリにローカル通知を追加することができます
次のメソッドを使用してcrea
-(void)saveNotification:(NSString *)description :(NSString *)notificationID :(BOOL)locationCheck {
// Create the notification info dictionary
// and set the notification ID string.
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[userInfo setObject:notificationID forKey:@"notification_id"];
// Setup the local notification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the notification ID and type data.
localNotification.userInfo = userInfo;
// Set the notification description.
localNotification.alertBody = [NSString stringWithFormat:@"%@", description];
// Set the sound alert MP3 file.
localNotification.soundName = [NSString stringWithFormat:@"Notification_sound_file.mp3"];
// Set the date for the notification or set the
// location depending on the notification type.
if (locationCheck == NO) {
// Fire date of your choice.
NSDate *yourFireDate;
// Set the reminder date.
double interval = [yourFireDate timeIntervalSinceNow];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:interval];
localNotification.timeZone = [NSTimeZone systemTimeZone];
// Set the notifcation repeat interval.
localNotification.repeatInterval = 0; // No repeat.
//localNotification.repeatInterval = NSCalendarUnitHour; // Every hour.
//localNotification.repeatInterval = NSCalendarUnitDay; // Every day.
//localNotification.repeatInterval = NSCalendarUnitWeekOfYear; // Once a week.
//localNotification.repeatInterval = NSCalendarUnitMonth; // Once a month.
//localNotification.repeatInterval = NSCalendarUnitYear; // Once a year.
}
else if (locationCheck == YES) {
// Set the locaton to the selected address co-ordinates.
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(latitude, longitude);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinates radius:100 identifier:[NSString stringWithFormat:@"region_%@", notificationID]];
// Set the notification to be presented
// when the user arrives at the location.
[region setNotifyOnEntry:YES];
[region setNotifyOnExit:NO];
// Set the notification location data.
[localNotification setRegion:region];
[localNotification setRegionTriggersOnce:NO];
}
// Save the local notification.
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
このメソッドを使用するには、独自のIDを作成する必要があります。このIDは、通知を区別するのに役立ちます(通知に応じて特定のアクションを実行する必要がある場合)。
あなたはそうのような上記のメソッドを呼び出すことができます。
[self saveNotification:@"test notification hello world" :@"unique id" :NO];
は(あなたは、ロケーションベースのローカルの通知を必要とする場合)あなたのdescried共同ordiantedでlatitude
とlongitude
を置き換えることを忘れないでください。
ステップ3
アプリは(フォアグラウンドまたはマルチタスク経由)現在開いている場合は、通知を処理するためには、アプリのデリゲートに別のメソッドを実装する必要があります。
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Check if a notifcation has been received.
if (application.applicationState == UIApplicationStateInactive) {
// You app in running in the background but will be active,
// should the user tap the iOS notification banner.
// Call your own custom method from here.
// Use [notification.userInfo valueForKey:@"notification_id"] to get the associated notification id (You will need to assign an ID, when creating the notification).
}
else if (application.applicationState == UIApplicationStateActive) {
// The app is open in the foreground
// you will need to display an alert or banner
// in your app to alert the user.
// Call your own custom method from here.
// Use [notification.userInfo valueForKey:@"notification_id"] to get the associated notification id (You will need to assign an ID, when creating the notification).
}
// Ensure the notifcation badge number is hidden.
application.applicationIconBadgeNumber = 0;
}
あなただけを設定することができます
を念頭に
:
-(void)deleteAllNotifications {
// Delete all the local notifications.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)deleteSpecificNotification:(NSString *)inputID {
// Get the notification(s) data.
NSArray *notificationData = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Loop through all the local notifcations and delete
// all the notifications that match the input id string.
for (int loop = 0; loop < [notificationData count]; loop++) {
// Get the notification object.
UILocalNotification *localNotification = [notificationData objectAtIndex:loop];
// If the notification id matches the input id then delete it.
if ([[localNotification.userInfo objectForKey:@"notification_id"] isEqualToString:inputID]) {
[[UIApplication sharedApplication] cancelLocalNotification: localNotification];
}
}
}
最初のGoogleは「iOSのローカル通知」のヒットは、[深さでのローカルおよびリモート通知]です( https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html) –