この問題に関する多くの投稿を読みましたが、どれも本当に役に立ちません。とても簡単にできるようですが、どういうわけか私はそれを理解することができません。
私はここで何をしようとしていますか? さて、アプリにプッシュ通知を送信します。すべて正常に動作します。バックグラウンド、非アクティブ、またはアプリが実行されていないときでも処理できます。しかし、もう 1 つの状態 (フォアグラウンドのアプリ) は頭痛の種です。
私が使用しているこのコードスニペットがあります。大丈夫だ。アプリがフォアグラウンドで実行されている場合、ユーザーは UIAlertview を取得します。しかし、ビュー ボタンにアクションを追加するにはどうすればよいでしょうか。私が意味するこの特定のアクション: 誰かが表示ボタンをタップすると、プッシュ通知を通過した URL にリダイレクトされます。
コードスニペットで以前に使用したのと同じように(正確にはこの部分):
NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
これが理にかなっており、誰かが私を助けてくれることを願っています。私はこれで迷っています...これは、この主題に関連するコードスニペット全体です。
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(@"%@",url);
}
/**
* Remote Notification Received while application was open.
*/
- (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);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
// app was already in the foreground
if ( application.applicationState == UIApplicationStateActive ) {
UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
message: alert
delegate: self
cancelButtonTitle:@"View"
otherButtonTitles: @"Cancel", nil];
[alertPush show];
[alertPush release];
}
// app is inactive or in the background
else {
[self Redirect:notifUrl];
}
#endif
}
// what to do if user taps the viewbutton in push notification alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
NSLog(@"View Button has been tapped");
// We need the url that has been passed by the push notification
}
else {
// Do something
}
}