0

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

有谁知道如何触发它?

4

3 回答 3

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
于 2014-11-12T06:02:27.887 回答
0

应用程序在首次启动时注册推送通知 (APN)。一旦它已经启动,你就不能让它用一个开关来初始化 APNs。但是,您可以对您的应用程序进行编码,以便在收到 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
}
于 2013-05-08T01:34:27.963 回答
0

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

于 2013-05-07T20:04:35.733 回答