在iOS

时间:2016-06-27 20:08:39

标签: php ios objective-c apple-push-notifications

谷歌推出具有iOS功能的新 FCM SDK 后,我很高兴收到来自控制台的通知,无论应用程序处于后台还是处于活动状态,我现在正试图让我的服务器发送提到的推送通知here我按照https://firebase.google.com/docs/cloud-messaging/server中提到的文档步骤进行了跟进,但结果是:

  

{" multicast_id":XXXXXXXXXXXXXXX,"成功":0,"失效":1," canonical_ids":0,&# 34;结果":[{"错误":" InvalidRegistration"}]}

PHP脚本

<?php
define( 'API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$registrationIds = array('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'

);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json',
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

此代码在github https://gist.github.com/prime31/5675017上找到,但我做了一些修改以适应我的情况

在搜索堆栈上的相关问题后,我发现了以下Notification in iOS using new Firebase Messaging SDKInvalid Registration Id with my gcm php server,但他们无法帮我解决问题。

  

这里是cliend方面,但正如我之前提到的,应用程序从控制台收到通知。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    float ver = [[[UIDevice currentDevice] systemVersion] floatValue];

        if(ver >= 8 && ver<9)
        {
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
            {
                [[UIApplication sharedApplication] registerForRemoteNotifications];
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

            }
        }else if (ver >=9){

            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

            [[UIApplication sharedApplication] registerForRemoteNotifications];


        }
        else{
            //iOS6 and iOS7 specific code
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];

        }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
                                                     name:kFIRInstanceIDTokenRefreshNotification object:nil];
        [FIRApp configure];
        [self connectToFcm];




        // Override point for customization after application launch.
        return YES;
    }



    - (void)tokenRefreshNotification:(NSNotification *)notification {
        // Note that this callback will be fired everytime a new token is generated, including the first
        // time. So if you need to retrieve the token as soon as it is available this is where that
        // should be done.
        NSString *refreshedToken = [[FIRInstanceID instanceID] token];
        NSLog(@"InstanceID token: %@", refreshedToken);
    }




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


        NSDictionary *newUserInfo = [userInfo objectForKey:@"notification"];

        NSLog(@"the user info is : %@",newUserInfo);

        UIApplicationState state = [[UIApplication sharedApplication] applicationState];

        if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
        {
            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.userInfo = newUserInfo;
            localNotification.alertTitle = [newUserInfo objectForKey:@"title"];
            localNotification.alertBody = [newUserInfo objectForKey:@"body"];
            localNotification.applicationIconBadgeNumber = 0;
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            localNotification.fireDate = [NSDate date];
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }



        else if (state == UIApplicationStateActive) {


            AudioServicesPlaySystemSound(1054);
            UIImage *icon = [UIImage imageNamed:@"placeholderimage"];
            NSString *title  = [newUserInfo objectForKey:@"title"];
            NSString *body = [newUserInfo objectForKey:@"body"];


            notification = [MPGNotification notificationWithTitle:title subtitle:body backgroundColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0f] iconImage:icon];
            notification.duration = 5.0;
            notification.swipeToDismissEnabled = NO;
            [notification show];
        }
    }

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"content---%@", token);
    }



    - (void)connectToFcm {
        [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Unable to connect to FCM. %@", error);
            } else {
                NSLog(@"Connected to FCM.");
            }
        }];
    }

0 个答案:

没有答案