简体   繁体   English

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

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

I have some problems receiving push notifications under IOS. 我在IOS下接收推送通知时遇到一些问题。 the didReceiveRemoteNotification fires but somewhere my code crashesh in the NSNotificationCenter. didReceiveRemoteNotification会触发,但是我的代码在NSNotificationCenter中崩溃了。 That's not the problem but basically I even don't receive nothing on the phone. 那不是问题,但基本上我什至没有收到任何电话。 Is the didReceiveRemoteNotification implementation correct ? didReceiveRemoteNotification实现是否正确? or I missed something ? 还是我错过了什么?

This is what I send: 这是我发送的:

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

And this the source. 而这个来源。

- (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 

Since your aps does not hold any data not notification is shown to the user. 由于您的aps不保存任何数据,因此不会向用户显示通知。 The -application:didReceiveRemoteNotification is only called if your app is in the foreground, not when it is in the background. 仅在您的应用程序位于前台时才调用-application:didReceiveRemoteNotification ,而不是在后台时才调用-application:didReceiveRemoteNotification

For this you need to use application:didReceiveRemoteNotification:fetchCompletionHandler: , but this is only available on iOS 7. 为此,您需要使用application:didReceiveRemoteNotification:fetchCompletionHandler:但这仅在iOS 7上可用。

If you want to handle push notification in the background then implement the above mentioned method and register that you app want to do remote-notification in the background. 如果要在后台处理推式通知,请实施上述方法并注册您的应用要在后台进行remote-notification

There are many thing to keep in mind when doing this, see the comment in the Apple documentation of application:didReceiveRemoteNotification:fetchCompletionHandler: 进行此操作时要记住很多事情,请参阅Apple文档application:didReceiveRemoteNotification:fetchCompletionHandler:

Implement this method if your app supports the remote-notification background mode. 如果您的应用支持remote-notification后台模式,请实施此方法。 This method is intended as a means for apps to minimize the time that elapses between the user seeing a push notification and the app displaying the associated data. 此方法旨在作为应用程序的一种方式,以最大程度地减少用户看到推送通知与应用程序显示关联数据之间的时间。 When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. 当推送通知到达时,系统会向用户显示通知,并在后台启动应用程序(如果需要),以便可以调用此方法。 Use this method to download any data related to the push notification. 使用此方法下载与推送通知相关的所有数据。 When your method is done, call the block in the handler parameter. 方法完成后,在handler参数中调用该块。

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. 与application:didReceiveRemoteNotification:方法不同,该方法仅在您的应用程序运行时才被调用,而无论应用程序的状态如何,系统都会调用此方法。 If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. 如果您的应用程序已挂起或未运行,则系统会在调用该方法之前唤醒或启动您的应用程序并将其置于后台运行状态。 If the user opens your app from the system-displayed alert, the system calls this method again so that you know which notification the user selected. 如果用户从系统显示的警报中打开您的应用程序,则系统会再次调用此方法,以便您知道用户选择了哪个通知。

When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. 调用此方法时,您的应用最多有30秒的挂钟时间来执行下载操作并调用指定的完成处理程序块。 In practice, your app should call the handler block as soon as possible after downloading the needed data. 实际上,您的应用程序应在下载所需数据后尽快调用处理程序块。 If you do not call the handler in time, your app is terminated. 如果您没有及时调用处理程序,则您的应用程序将终止。 More importantly, the system uses the elapsed time to calculate power usage and data costs for your app's background downloads. 更重要的是,系统使用经过的时间来计算应用程序后台下载的功耗和数据成本。

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

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