对于将本地通知设置为:
// Schedule the notification
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:Notitype];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
//Create user info for local notification.
NSDictionary* dict = @{@"Type": @"message", @"ID": @"set your unique ID", @"body": notification.alertBody,};
notification.userInfo = dict;
//You can Set Type as: message, friend Request, video call, Audio call.
[[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
要获取单击哪种类型的本地通知,您可以使用didReceiveLocalNotification委托。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//[super application:application didReceiveLocalNotification:notification]; // In most case, you don't need this line
//Get notification type
NSString *notificationType = [notification.userInfo valueForKey:@"Type"];
//notificationType as: message, friend Request, video call, Audio call.
NSString *notificationBody = [notification.userInfo valueForKey:@"Notification_body"];
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// Application in foreground when notification is delivered.
if ([notificationType isEqual:@"message"]) {
//Handle message
} else if ([notificationType isEqual:@"friend Request"]) {
//Handle friend Request
} else if ([notificationType isEqual:@"video call"]) {
//Handle video call
} else if ([notificationType isEqual:@"Audio call"]) {
//Handle Audio call
}
} else if (state == UIApplicationStateBackground) {
// Application was in the background when notification was delivered.
if ([notificationType isEqual:@"message"]) {
//Handle message
} else if ([notificationType isEqual:@"friend Request"]) {
//Handle friend Request
} else if ([notificationType isEqual:@"video call"]) {
//Handle video call
} else if ([notificationType isEqual:@"Audio call"]) {
//Handle Audio call
}
} else {
}
}
更新
还要检查同样的事情DidFinishLaunchingWithOptions,可能的用户不会对通知做出反应,而是点击应用程序图标打开应用程序。因此,如果是视频通话或任何紧急情况,则必须通过DidFinishLaunchingWithOptions