PHP代码如何使用APNS设备令牌进行Apple推送通知?

时间:2018-04-04 00:27:22

标签: apple-push-notifications apns-php

我在这里查看PHP代码:

https://gist.github.com/valfer/18e1052bd4b160fed86e6cbb426bb9fc

看起来不错。我喜欢用它。但我对此感到困惑:

 * @param $token            the token of the device

所以我需要设备令牌?对于要在服务器上运行的PHP代码?如何获取设备令牌?

1 个答案:

答案 0 :(得分:-1)

启动iOS应用程序后,它会在AppDelegate的{​​{1}}中使用以下代码为Apple Push Notifications注册自己。

didFinishLaunchingWithOptions

然后根据成功或失败可以调用以下委托方法

if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
        //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}

以上所有逻辑应在项目的-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSString* strdeviceToken = [[NSString alloc]init]; strdeviceToken=[self stringWithDeviceToken:deviceToken]; NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:strdeviceToken forKey:PREF_DEVICE_TOKEN]; [prefs synchronize]; NSLog(@"My token is===========> : %@",strdeviceToken); } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { // NSLog(@"Failed to get token, error: %@", error); NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:@"" forKey:PREF_DEVICE_TOKEN]; [prefs synchronize]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if (application.applicationState == UIApplicationStateActive) { // [self showToastMessage:@"Active"]; } else if (application.applicationState == UIApplicationStateBackground) { // [self showToastMessage:@"Background"]; } else if (application.applicationState == UIApplicationStateInactive) { // [self showToastMessage:@"Inactive"]; } // [self handleIncomingNotification:userInfo delay:0.0]; } 类中处理。然后,您可以在要从iOS调用的PHP代码中进行一些API调用,并将此设备令牌发送到服务器上,并将其保存以备将来使用。

相关问题