繁体   English   中英

在手机上未在应用程序上接收推送IOS通知时出现问题

[英]Problems receiving push IOS notifications on Phone not on App

我在IOS下接收推送通知时遇到一些问题。 didReceiveRemoteNotification会触发,但是我的代码在NSNotificationCenter中崩溃了。 那不是问题,但基本上我什至没有收到任何电话。 didReceiveRemoteNotification实现是否正确? 还是我错过了什么?

这是我发送的:

 "aps":[],"custom":["hello IOS","0","48.213822","16.389186","1.39593410094E+12",null]}

而这个来源。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

if (isAttentionViewOpen) {

    NSLog(@"Sent Notification to view!");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PushIn" object:self userInfo: userInfo];
}

}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

    //#if !TARGET_IPHONE_SIMULATOR
        NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
        NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

        NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

        NSString *pushBadge = @"disabled";
        NSString *pushAlert = @"disabled";
        NSString *pushSound = @"disabled";

        if(rntypes == UIRemoteNotificationTypeBadge){
            pushBadge = @"enabled";
        }
        else if(rntypes == UIRemoteNotificationTypeAlert){
            pushAlert = @"enabled";
        }
        else if(rntypes == UIRemoteNotificationTypeSound){
            pushSound = @"enabled";
        }
        else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)){
            pushBadge = @"enabled";
            pushAlert = @"enabled";
        }
        else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)){
            pushBadge = @"enabled";
            pushSound = @"enabled";
        }
        else if(rntypes == ( UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)){
            pushAlert = @"enabled";
            pushSound = @"enabled";
        }
        else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)){
            pushBadge = @"enabled";
            pushAlert = @"enabled";
            pushSound = @"enabled";
        }

        // Get the users Device Model, Display Name, Unique ID, Token & Version Number
        UIDevice *dev = [UIDevice currentDevice];

        CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
        NSString *uuidStr = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
        CFRelease(uuidObject);

        deviceUuid = uuidStr;// dev.uniqueIdentifier;
        NSString *deviceName = dev.name;
        NSString *deviceModel = dev.model;
        NSString *deviceSystemVersion = dev.systemVersion;

        // Prepare the Device Token for Registration (remove spaces and < >)
        NSString *deviceToken = [[[[devToken description]
                                   stringByReplacingOccurrencesOfString:@"<"withString:@""]
                                  stringByReplacingOccurrencesOfString:@">" withString:@""]
                                 stringByReplacingOccurrencesOfString: @" " withString: @""];
       // SENT IT TO SERVER 

由于您的aps不保存任何数据,因此不会向用户显示通知。 仅在您的应用程序位于前台时才调用-application:didReceiveRemoteNotification ,而不是在后台时才调用-application:didReceiveRemoteNotification

为此,您需要使用application:didReceiveRemoteNotification:fetchCompletionHandler:但这仅在iOS 7上可用。

如果要在后台处理推式通知,请实施上述方法并注册您的应用要在后台进行remote-notification

进行此操作时要记住很多事情,请参阅Apple文档application:didReceiveRemoteNotification:fetchCompletionHandler:

如果您的应用支持remote-notification后台模式,请实施此方法。 此方法旨在作为应用程序的一种方式,以最大程度地减少用户看到推送通知与应用程序显示关联数据之间的时间。 当推送通知到达时,系统会向用户显示通知,并在后台启动应用程序(如果需要),以便可以调用此方法。 使用此方法下载与推送通知相关的所有数据。 方法完成后,在handler参数中调用该块。

与application:didReceiveRemoteNotification:方法不同,该方法仅在您的应用程序运行时才被调用,而无论应用程序的状态如何,系统都会调用此方法。 如果您的应用程序已挂起或未运行,则系统会在调用该方法之前唤醒或启动您的应用程序并将其置于后台运行状态。 如果用户从系统显示的警报中打开您的应用程序,则系统会再次调用此方法,以便您知道用户选择了哪个通知。

调用此方法时,您的应用最多有30秒的挂钟时间来执行下载操作并调用指定的完成处理程序块。 实际上,您的应用程序应在下载所需数据后尽快调用处理程序块。 如果您没有及时调用处理程序,则您的应用程序将终止。 更重要的是,系统使用经过的时间来计算应用程序后台下载的功耗和数据成本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM