简体   繁体   中英

Problems receiving push IOS notifications on Phone not on App

I have some problems receiving push notifications under IOS. the didReceiveRemoteNotification fires but somewhere my code crashesh in the NSNotificationCenter. That's not the problem but basically I even don't receive nothing on the phone. Is the didReceiveRemoteNotification implementation correct ? 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. The -application:didReceiveRemoteNotification is only called if your app is in the foreground, not when it is in the background.

For this you need to use application:didReceiveRemoteNotification:fetchCompletionHandler: , but this is only available on 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.

There are many thing to keep in mind when doing this, see the comment in the Apple documentation of application:didReceiveRemoteNotification:fetchCompletionHandler:

Implement this method if your app supports the remote-notification background mode. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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