Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS和本地通知_Ios_Objective C_Swift_Uilocalnotification_Nsnotificationcenter - Fatal编程技术网

iOS和本地通知

iOS和本地通知,ios,objective-c,swift,uilocalnotification,nsnotificationcenter,Ios,Objective C,Swift,Uilocalnotification,Nsnotificationcenter,是否有方法从应用程序向应用程序发送本地通知 我需要每天早上向我的应用程序用户发送通知。因此,我是否可以向应用程序添加一些代码,以便在用户启动后,每天早上他或她都会收到徽章/通知?您可以通过执行以下操作向iOS应用程序添加本地通知: 第一步 在应用程序代理中注册本地通知: -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

是否有方法从应用程序向应用程序发送本地通知


我需要每天早上向我的应用程序用户发送通知。因此,我是否可以向应用程序添加一些代码,以便在用户启动后,每天早上他或她都会收到徽章/通知?

您可以通过执行以下操作向iOS应用程序添加本地通知:

第一步

在应用程序代理中注册本地通知:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Register the app for local notifcations.

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
    }

    // Setup the local notification check.
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    // Check if a notifcation has been received.

    if (notification) {

        dispatch_async(dispatch_get_main_queue(), ^{

            // Run the notifcation.
            // 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;

    return YES;
}
第二步

使用以下方法创建本地通知:

-(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];
不要忘记将
纬度
经度
替换为您描述的坐标(如果您需要基于位置的本地通知)

第三步

如果应用程序当前处于打开状态(在前台或通过多任务处理),您需要在应用程序代理中实现另一种方法,以便处理通知:

-(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;
}
需要记住的其他几点

  • 您最多只能设置64个本地通知。 (重复的通知被视为一个本地通知)
  • 本地通知不能有fireDate和位置区域。如果希望在给定的时间和位置显示相同的通知,则必须创建两个具有相同描述的单独本地通知(一个带有日期,另一个带有位置)
您可以使用以下方法删除所有本地通知(或特定通知):

-(无效)删除所有通知{
//删除所有本地通知。
[[UIApplication sharedApplication]设置应用程序徽章编号:0];
[[UIApplication sharedApplication]取消所有本地通知];
}
-(无效)deleteSpecificNotification:(NSString*)输入{
//获取通知数据。
NSArray*notificationData=[[UIApplication sharedApplication]ScheduledLocalNotification];
//循环浏览所有本地通知并删除
//与输入id字符串匹配的所有通知。
for(int循环=0;循环<[notificationData count];循环++){
//获取通知对象。
UILocalNotification*localNotification=[notificationData对象索引:循环];
//如果通知id与输入id匹配,则将其删除。
if([[localNotification.userInfo objectForKey:@“notification_id”]IsequalString:inputID]){
[[UIApplication sharedApplication]取消本地通知:本地通知];
}
}
}
iOS本地通知在谷歌的首个热门搜索是
-(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];
        }
    }
}