15

これまで見てきたことはすべて、プッシュ通知アラートを自分のAppDelegate. viewControllerただし、私のアプリでは、ユーザーが登録プロセスを実行する必要があり、登録プロセスが完了した後に表示されるにユーザーが到着しない限り、プッシュ通知を受け取りたいかどうかをユーザーに尋ねたくありません。

viewDidLoadこのコードの一部を、アプリ デリゲート以外のビュー コントローラーのメソッドに入れることはできますか? 下の 2 つのメソッド " didRegisterForRemoteNotificationsWithDeviceToken" と " didReceiveRemoteNotification" をアプリ デリゲートに残しておく必要がありますか? それとも、リモート通知を登録しようとしている場所に移動する必要がありますか?

以下のコード ブロックを使用して、アプリでプッシュ通知を登録しています。

アプリデリゲートの didFinishLaunchingWithOptions メソッドで:

[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound];

アプリのデリゲートに追加されたメソッド:

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Store the deviceToken
}

- (void)application:(UIApplication *)application 
        didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //handle push notification
}

私が訪れたリソースは、このコードブロックが

4

4 に答える 4

19

登録呼び出しはいつでも実行できます。ユーザーにプッシュ通知を受信して​​ほしいことがアプリでわかっている場合にのみ実行することをお勧めします。

ただし、2 つのアプリケーション デリゲート コールバックはアプリ デリゲートに含まれている必要があります。これは、アプリケーション デリゲートで通知タイプを登録し、1 つしかないためです。アプリケーションデリゲートメソッドを呼び出して登録を行うことをお勧めします。ビューコントローラーから呼び出すことができます[[UIApplication sharedApplication] delegate](その呼び出しの結果をアプリケーションデリゲートクラスにキャストします)。

于 2013-06-19T04:39:43.253 に答える
5

最善の方法は、削除通知を処理するアプリのデリゲート メソッドで、次を使用して通知を送信することです。NSNotificationCenter

[[NSNotificationCenter defaultCenter] postNotification:@"remoteNotification" withObject:whateverYouWantHere];

次に、 を使用しNSNotificationCenterて、関心のある UIViewControllers をremoteNotification通知のオブザーバーとして追加します。

于 2013-06-19T06:16:56.967 に答える
1

SWIFT 3 以上

AppDelegate の外部からプッシュ通知を呼び出すには

import UserNotifications

class HomeViewController: UIViewController
{
    override func viewDidLoad() {
        super.viewDidLoad()

        let application = UIApplication.shared

        registerPushNotification(application)
    }



    func registerPushNotification(_ application: UIApplication){

            UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in

                if granted {
                    print("Notification: Granted")

                } else {
                    print("Notification: not granted")

                }
            }

            application.registerForRemoteNotifications()
        }

}



extension HomeViewController{

    // Called when APNs has assigned the device a unique token
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Convert token to string
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

        // Print it to console
        print("APNs device token: \(deviceTokenString)")

        // Persist it in your backend in case it's new
    }

    // Called when APNs failed to register the device for push notifications
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        // Print the error to console (you should alert the user that registration failed)
        print("APNs registration failed: \(error)")
    }

    // Push notification received
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
        // Print notification payload data
        print("Push notification received: \(data)")
    }
}
于 2017-10-10T14:57:57.913 に答える