如何将EasyAPNS实施到iOS 8?

时间:2014-11-10 23:08:25

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

我正在尝试在iOS 8的项目中使用EasyAPNS。但由于项目已经过时且没有更新,我遇到了问题。

更具体地说,关于服务器,虽然它被正确配置为教程视频Caixeta Raphael,但在日志文件中出现以下问题:

  

[10-Nov-2014 20:18:03 America / Sao_Paulo] PHP致命错误:推送徽章   必须是启用或禁用。

     

1)APNS :: __ construct - >文件:apns.php(第55行)2)   APNS :: _ registerDevice - >文件:class_APNS.php(第252行)3)   APNS :: _ triggerError - >文件:class_APNS.php(第319行)

     

在第706行的/home/pensesof/public_html/apns/classes/class_APNS.php

有人在当前项目中使用或正在使用EasyAPNS吗?

2 个答案:

答案 0 :(得分:0)

我实现了在iOS8中使用的AppDelegate.m。

首先你必须添加框架:AudioToolbox.framework

Go Targets>常规...向下滚动链接框架和库并单击加号按钮,然后搜索AudioToolbox.framework

复制并粘贴AppDelgate.m的代码后:

#import "AppDelegate.h"
#import <AudioToolBox/AudioToolbox.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    //register to receive notifications
    // Add registration for remote notifications

    //[[UIApplication sharedApplication]

    // registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    // For iOS 8:
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
     UIUserNotificationTypeBadge |
     UIUserNotificationTypeSound
                                      categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    // Clear application badge when app launches
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    application.applicationIconBadgeNumber = 0;


      return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



#pragma mark - Configuração APNS

/*
 2
 * ------------------------------------------------------------------------------------------
 3
 *  BEGIN APNS CODE
 4
 * ------------------------------------------------------------------------------------------
 5
 */



/**
 8
 * Fetch and Format Device Token and Register Important Information to Remote Server
 9
 */

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



#if !TARGET_IPHONE_SIMULATOR


    // Get Bundle Info for Remote Registration (handy if you have more than one app)

   // NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];

   NSString *appName = @"TesteAPNS1";

    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


    // Check what Notifications the user has turned on.  We registered for all three, but they may have manually disabled some or all of them.

   // NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];


    UIRemoteNotificationType rntypes = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    // Set the defaults to disabled unless we find otherwise...

    NSString *pushBadge = @"disabled";

    NSString *pushAlert = @"disabled";

    NSString *pushSound = @"disabled";



    // Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which

    // one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the

    // single notification types will only match if they are the ONLY one enabled.  Likewise, when we are checking for a pair of notifications, it will only be

    // true if those two notifications are on.  This is why the code is written this way

    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];

    //NSString *deviceUuid = dev.uniqueIdentifier;
    NSString *deviceUuid = [[[UIDevice currentDevice] identifierForVendor ]UUIDString];// Substitui o codigo acima

    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: @""];



    // Build URL String for Registration

    // !!! CHANGE "www.mywebsite.com" TO YOUR WEBSITE. Leave out the http://

    // !!! SAMPLE: "secure.awesomeapp.com"

   #pragma mark - Setup Your URL here!

    NSString *host = @"www.mywebsite.com";



#pragma mark - CHANGE "/apns.php?" TO THE PATH TO WHERE apns.php IS INSTALLED

    // !!! ( MUST START WITH / AND END WITH ? ).

    // !!! SAMPLE: "/path/to/apns.php?"

    NSString *urlString = [@"/path/to/apns.php?"stringByAppendingString:@"task=register"];

    #pragma mark - Start add propertys to url

    urlString = [urlString stringByAppendingString:@"&appname="];

    urlString = [urlString stringByAppendingString:appName];

    urlString = [urlString stringByAppendingString:@"&appversion="];

    urlString = [urlString stringByAppendingString:appVersion];

    urlString = [urlString stringByAppendingString:@"&deviceuid="];

    urlString = [urlString stringByAppendingString:deviceUuid];

    urlString = [urlString stringByAppendingString:@"&devicetoken="];

    urlString = [urlString stringByAppendingString:deviceToken];

    urlString = [urlString stringByAppendingString:@"&devicename="];

    urlString = [urlString stringByAppendingString:deviceName];

    urlString = [urlString stringByAppendingString:@"&devicemodel="];

    urlString = [urlString stringByAppendingString:deviceModel];

    urlString = [urlString stringByAppendingString:@"&deviceversion="];

    urlString = [urlString stringByAppendingString:deviceSystemVersion];

    urlString = [urlString stringByAppendingString:@"&pushbadge="];

    urlString = [urlString stringByAppendingString:pushBadge];

    urlString = [urlString stringByAppendingString:@"&pushalert="];

    urlString = [urlString stringByAppendingString:pushAlert];

    urlString = [urlString stringByAppendingString:@"&pushsound="];

    urlString = [urlString stringByAppendingString:pushSound];


 #pragma mark - Register the device data in server.Fire URL with data device
    // Register the Device Data

    // !!! CHANGE "http" TO "https" IF YOU ARE USING HTTPS PROTOCOL

    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:urlString];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSLog(@"Register URL: %@", url);

    NSLog(@"Return Data: %@", returnData);



#endif

}



/**
 113
 * Failed to Register for Remote Notifications
 114
 */

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {



#if !TARGET_IPHONE_SIMULATOR



    NSLog(@"Error in registration. Error: %@", error);



#endif

}



/**
 125
 * Remote Notification Received while application was open.
 126
 */

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



#if !TARGET_IPHONE_SIMULATOR



    NSLog(@"remote notification: %@",[userInfo description]);

    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];



    NSString *alert = [apsInfo objectForKey:@"alert"];

    NSLog(@"Received Push Alert: %@", alert);



    NSString *sound = [apsInfo objectForKey:@"sound"];

    NSLog(@"Received Push Sound: %@", sound);

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);



    NSString *badge = [apsInfo objectForKey:@"badge"];

    NSLog(@"Received Push Badge: %@", badge);

    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];



#endif

}
@end

答案 1 :(得分:0)

//Check Notifications user has turned on.
//Registered all three, but they may have manually disabled some or all of them.
NSUInteger rntypes;

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
    //returns UIRemoteNotificationType typedef for nsuinteger
    UIUserNotificationSettings * notificationSettings;
    notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    rntypes = [notificationSettings types];
}
else
{
    rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
}


// Set the defaults to disabled unless we find otherwise...
NSString * pushBadge = (rntypes & UIRemoteNotificationTypeBadge) ? @"enabled" : @"disabled";
NSString * pushAlert = (rntypes & UIRemoteNotificationTypeAlert) ? @"enabled" : @"disabled";
NSString * pushSound = (rntypes & UIRemoteNotificationTypeSound) ? @"enabled" : @"disabled";