Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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/5/objective-c/22.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_Uilocalnotification_Nsnotificationcenter - Fatal编程技术网

iOS-收到消息时触发本地通知

iOS-收到消息时触发本地通知,ios,objective-c,uilocalnotification,nsnotificationcenter,Ios,Objective C,Uilocalnotification,Nsnotificationcenter,我正在开发一个应用程序,允许它的用户互相发送消息。收到新消息时,它将显示在当前用户的表视图中。我希望我的应用程序在新消息到达时向当前用户发送通知。有人知道我该怎么做吗 我已在AppDelegate中设置了通知: appdelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Let the dev

我正在开发一个应用程序,允许它的用户互相发送消息。收到新消息时,它将显示在当前用户的表视图中。我希望我的应用程序在新消息到达时向当前用户发送通知。有人知道我该怎么做吗

我已在AppDelegate中设置了通知:

appdelegate.m

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

    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
我知道以下代码将允许我在指定时间(例如,由选择器设置)发出通知:

但是,当登录用户的新消息发布到服务器(并出现在tableview中)时,如何发出通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];}
如果您的系统是iOS8,则需要注册权限

UILocalNotification * notification = [[UILocalNotification alloc] init];
    NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:Time];
    if (notification!=nil) {


        notification.fireDate= pushDate;

        notification.timeZone = [NSTimeZone defaultTimeZone];

        notification.repeatInterval = kCFCalendarUnitDay;

        notification.soundName = UILocalNotificationDefaultSoundName;

        notification.alertBody = @"Time O.K!";
        NSLog(@"Ready");

        notification.applicationIconBadgeNumber = 1;
        //notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;

        NSDictionary * inforDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        notification.userInfo =inforDic;

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

您可以尝试一下,我可以在我的设备上成功使用此代码。

当出现新消息时,您需要向应用程序发送推送通知。您可以直接显示推送通知,也可以发送“静默”通知并使用此通知生成本地通知hi@Paulw11-我已成功设置推送通知(使用本教程:)-我只是不确定如何仅在发布新消息时触发它?现在,我正在从终端手动触发它们。通常情况下,您可以从服务器端处理它们,尽管您可以让发送应用程序实例执行此操作,但这是不可取的。不,您不需要设置推送通知。如果应用程序在运行时收到这些消息,则可以使用本地通知。您需要实现
应用程序:didReceiveLocalNotification:
method@DuncanC怎样?!Lol.是的,这些通知是在应用程序运行时发生的;但是,我可以使用哪一段代码来表示,例如,“当收到新消息时,触发通知X”我很困惑-这与上面发布的代码不一样吗(仅当提供了特定时间时才通知)?您可以使用计时器在特定时间触发本地通知。
UILocalNotification * notification = [[UILocalNotification alloc] init];
    NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:Time];
    if (notification!=nil) {


        notification.fireDate= pushDate;

        notification.timeZone = [NSTimeZone defaultTimeZone];

        notification.repeatInterval = kCFCalendarUnitDay;

        notification.soundName = UILocalNotificationDefaultSoundName;

        notification.alertBody = @"Time O.K!";
        NSLog(@"Ready");

        notification.applicationIconBadgeNumber = 1;
        //notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;

        NSDictionary * inforDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        notification.userInfo =inforDic;

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }