UISwitch を使用して、プッシュ通知を有効/無効にしたいと考えています。Tweetbot のように。
誰かがそれをトリガーする方法を知っていますか?
UISwitch を使用して、プッシュ通知を有効/無効にしたいと考えています。Tweetbot のように。
誰かがそれをトリガーする方法を知っていますか?
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
アプリは、最初の起動時にプッシュ通知 (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];
}
NSNotification "ReceivedNotificationAlert" を使用して、UISwitch を使用して何かを実行するかどうかを指定できます。例えば:
if(switchAPNprocess.on){
// process APN
}
else {
// ignore APN
}
アプリケーションから直接行うことはできません。これを行うには、UISwitch に情報をバックエンドに送信させ、この情報をデータベースに保存し、このユーザーへのプッシュ通知の送信を停止する必要があります。