Je souhaite planifier des notifications locales à l'aide d'iOS 10. J'aimerais savoir comment procéder. J'ai regardé partout sur le Web, mais je ne trouve des indices que pour enregistrer et gérer les notifications. Pas pour la planification d'une notification locale.
Alors, quelqu'un sait-il comment faire cela?
Essayez-le. Son code obsolète mais fonctionnel. Utilisez-le pour avant iOS 10.0:
//Get all previous noti..
NSLog(@"scheduled notifications: --%@----", [[UIApplication sharedApplication] scheduledLocalNotifications]);
NSDate *now = [NSDate date];
now = [now dateByAddingTimeInterval:60*60*24*7]; //7 for 7th day of the week.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *SetAlarmAt = [calendar dateFromComponents:components];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = SetAlarmAt;
NSLog(@"FIRE DATE --%@----",[SetAlarmAt description]);
localNotification.alertBody =@"Alert";
localNotification.alertAction = [NSString stringWithFormat:@"My test for Weekly alarm"];
localNotification.userInfo = @{
@"alarmID":[NSString stringWithFormat:@"123"],
@"SOUND_TYPE":[NSString stringWithFormat:@"hello.mp3"]
};
localNotification.repeatInterval=0; //[NSCalendar currentCalendar];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Pour iOS 10.0 et versions ultérieures: essayez maintenant avec le cadre UserNotifications: ajoutez le cadre et importez comme #import. Dans la méthode Appdelegate Didfinishluanch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self testAlrt];
}
}];
Dans votre ibaction ou méthode, écrivez-la et testez:
NSDate *now = [NSDate date];
// NSLog(@"NSDate--before:%@",now);
now = [now dateByAddingTimeInterval:60*60*24*7];
NSLog(@"NSDate:%@",now);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *todaySehri = [calendar dateFromComponents:components]; //unused
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten"
content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
Suivez l'étape: 1. Importez UserNotifications.framework et accédez à votre classe AppDelegate.
En .h
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
Inscrivez-vous à Push:
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
Maintenant, ajoutez ceci dans le lancement:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self registerForRemoteNotifications];
return YES;
}
- (void)registerForRemoteNotifications {
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
else {
// Code for old versions
}
}
Déléguer des méthodes pour UserNotifications:
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
}
Droits de notifications push: à partir de l'onglet Capacités de la cible du projet et ajoutez Droits de notifications push.
Ajoutez correctement le certificat Push et mobile. J'espère que c'est tout ce dont vous avez besoin!
Pour plus d'informations: http://ashishkakkad.com/2016/09/Push-notifications-in-ios-10-objective-c/