2

この問題に関する多くの投稿を読みましたが、どれも本当に役に立ちません。とても簡単にできるようですが、どういうわけか私はそれを理解することができません。

私はここで何をしようとしていますか? さて、アプリにプッシュ通知を送信します。すべて正常に動作します。バックグラウンド、非アクティブ、またはアプリが実行されていないときでも処理できます。しかし、もう 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
        }
    }
4

1 に答える 1

4

notifUrlクラスの他のメソッドでもこれにアクセスできるように、.h ファイルで変数を定義するだけです。次に、その変数に url を格納し、それを alertview デリゲート メソッドで使用します。

//Add this in .h file
NSString *notifUrl;

//Add this in .m file

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

        //Define this variable in .h file so that you can access this in other methods as well
        notifUrl = [apsInfo objectForKey:@"url"];
        NSLog(@"Received Push Url: %@", notifUrl);
        [notifUrl retain];    // this is retain value of notifUrl so that you can use it later
        // 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");
            NSLog(@"Received Push Url: %@", notifUrl);
           [self Redirect:notifUrl];
        }
        else {
            // Do something
        }
    }
于 2013-09-12T12:26:31.450 に答える