通过UISwitch启用推送通知

时间:2013-05-07 19:50:15

标签: ios xcode push-notification

我想使用UISwitch来启用/禁用推送通知。就像Tweetbot一样。

有谁知道如何触发?

3 个答案:

答案 0 :(得分:1)

You can also do it in the following way.
create a IBOutlet for UISwitch  

@property (strong, nonatomic) IBOutlet *pushNotificationSwitch;

and in Action method, store the value in NSUserDefaults.


- (IBAction)pushNotificationSwitchChanged:(id)sender
{
    NSNumber *switch_value = [NSNumber numberWithBool:[self.pushNotificationSwitch isOn]];
    [[NSUserDefaults standardUserDefaults] setObject:switch_value forKey:RECIEVE_APNS];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

and check it in viewdidload.



- (void)viewDidLoad 
{
    [super viewDidLoad];


    // Do any additional setup after loading the view from its nib.

     NSNumber *sett = [[NSUserDefaults standardUserDefaults] valueForKey:RECIEVE_APNS];
    if( [sett boolValue] )
    {

        [self.pushNotificationSwitch setOn:YES];

    }
    else{
        [self.pushNotificationSwitch setOn:NO];
    }

}

and In AppDelegate.m, add the following code

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

{

    NSNumber *sett = [[NSUserDefaults standardUserDefaults] objectForKey:RECIEVE_APNS];
    if( [sett boolValue] )
    {
        int currentBadgeCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"BadgeCount"];

        //Set the baadge count on the app icon in the home screen
        int badgeValue = [[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] intValue];
        [UIApplication sharedApplication].applicationIconBadgeNumber = badgeValue + currentBadgeCount;
        [[NSUserDefaults standardUserDefaults] setInteger:badgeValue + currentBadgeCount forKey:@"BadgeCount"];

        NSString *alertString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
        NSString *playSoundOnAlert = [NSString stringWithFormat:@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"sound"]];
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],playSoundOnAlert]];
        NSError *error;

        if (alertString.length > 0)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"App Name" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
            audioPlayer.numberOfLoops = 1;
            [audioPlayer play];
            [alert show];
        }

    }
}

    enter code here

答案 1 :(得分:0)

您无法直接从应用程序执行此操作。如果您想这样做,您需要让UISwitch将信息发送到您的后端,将此信息存储在您的数据库中并停止向该用户发送推送通知。

答案 2 :(得分:0)

应用程序在首次启动时注册推送通知(APN)。一旦已经启动,您就无法使用交换机初始化APN。但是,您可以对应用程序进行编码,一旦收到APN,交换机就可以选择使用用户界面做“某事”。

例如,您可以使用以下代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
// do what you need with the data...
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedNotificationAlert" object:self];
}

您可以使用您的UISwitch使用NSNotification“ReceivedNotificationAlert”执行或不执行某些操作。例如:

if(switchAPNprocess.on){
// process APN
}
else {
// ignore APN
}