22

プッシュ通知を受信できるiphoneアプリがあります。現在、私はiPhoneの設定/通知に行くことによって私のアプリのプッシュ通知を無効にすることができます。アプリ内でIphoneプッシュ通知を有効または無効にする

しかし、プッシュ通知を有効または無効にするには、アプリ内にスイッチまたはボタンを追加します。

私はfoursqure iphoneアプリでそれを見たのでできます。彼らは、設定の呼び出し通知設定のセクションを持っていて、ユーザーはアプリに対して異なる種類の通知を有効または無効にすることができます。

私はこれを適切に解決するためにネット全体を見ていますが、それでも方法は見つけられません。どのようにすればよいのでしょうか?事前に

感謝:)

+0

は、このアロですアップルは結婚? – Anil

+0

@私も同じことを探していますが、これについてはまだ明確な答えがありません。 –

答えて

19

まず最初はアプリ内部であなたcan not enable and disablepush notificationということです。回避策を解決する必要があるアプリを見つけた場合は、回避策が必要です。

アプリ内で実行したい場合は、1つの識別子を使用してプッシュ通知の有効化と無効化ボタンに従ってサーバーに送信します。したがって、サーバー側のコーディングではこの識別子が使用され、それに従って動作します。識別子のように、そうでなければ通知を送信するサーバーよりも有効です。

ユーザーがenableまたはdisablePush Notificationsに設定されていることを確認するには、次のコードを使用します。

押して有効または無効にiPhoneの通知

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
if (types == UIRemoteNotificationTypeNone) 
// Yes it is.. 

希望、これは

+0

あなたはアイデアを得ました::)おかげさまです。ちょうどあなたのアイデアの解決策を思いつき、サーバにいくつかの値を送り、それらをデバイストークンに従って保存することができました。いくつかの列をデータベースに追加する必要がありますが、動作します。もう一度ありがとう:) –

+2

以下の回答として、unregisterForRemoteNotificationTypesを呼び出すと、プッシュ通知の配信が停止します。これは、ユーザーログアウト時に私のアプリケーションでこれをテストしても機能します。 –

30

..あなたに役立つ -

[がFYIほとんどのユーザーはそれが iOSの10で作業を停止したことを報告しています]もう一度registerForRemoteNotificationTypesunregisterForRemoteNotificationTypesを呼び出すと、アプリケーションでプッシュ通知を簡単に有効または無効にできます。私はこれを試して、それが動作します。

+0

これはAppleによって許可されていますか? – Anil

+0

はい!完全に許可される! –

+0

これはまだ通知センター設定の対象ですが、それは正しいのですか? – nmr

1

実際には、&プッシュ通知を登録および登録解除することでプッシュ通知を無効にすることができます。

プッシュ通知を有効にする:

if #available(iOS 10.0, *) { 
    // For iOS 10.0 + 
    let center = UNUserNotificationCenter.current() 
    center.delegate = self 
    center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in 
     if error == nil{ 
      DispatchQueue.main.async(execute: { 
       UIApplication.shared.registerForRemoteNotifications() 
      }) 
     } 
    } 
}else{ 
    // Below iOS 10.0 

    let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil) 
    UIApplication.shared.registerUserNotificationSettings(settings) 

    //or 
    //UIApplication.shared.registerForRemoteNotifications() 
} 

デリゲートメソッド

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

} 


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    // .. Receipt of device token 
} 


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    // handle error 
} 

を無効にプッシュ通知:

UIApplication.shared.unregisterForRemoteNotifications() 
+0

とすると、あなたのアプリケーションの設定内に有効/無効ボタンが1つあり、このコードを使って通知を有効/右 ?それは働いている?リンゴはこれを承認しますか? –

関連する問題