菜鸟教程小白 发表于 2022-12-13 13:37:02

php - 适用于 iOS 的 Google 云消息传递 MismatchSenderId


                                            <p><p>目前对 IOS 的 GCM 推送通知存在问题。</p>

<p>我尝试了不同的教程,但仍然无法通过 <code>MismatchSenderid</code>。</p>

<p>我使用的代码显示在输出下方 -</p>

<pre><code>$msg = array
    (
            &#39;to&#39;       =&gt; $registatoin_ids,
            &#39;notification&#39;         =&gt; array(&#39;subtitle&#39;      =&gt; &#39;Alert message!&#39;,
            &#39;badge&#39;       =&gt; 1,
            &#39;sound&#39;         =&gt; &#39;default&#39;,
            &#34;title&#34; =&gt;&#34;Notification&#34;,
            &#39;body&#39;   =&gt; $message)
    );

=&gt; Array
    (
       =&gt; ldvBkaxZ0y4:APA91bGP7SWNFZg_BQIum0zKLKyQ6cDZmYi7pQvA5P9ZEjTiI8qiSU7hgX3qL5WnqzDEPTqCfQnqlKAXZ0-0pZkmr6omZO3eI1aAis_R1EaZPdMdtxx_28pkplmuPY2vX1oErkbCuZVB
    )

=&gt; Array
    (
       =&gt; Array
            (
                =&gt; ldvBkaxZ0y4:APA91bGP7SWNFZg_BQIum0zKLKyQ6cDZmYi7pQvA5P9ZEjTiI8qiSU7hgX3qL5WnqzDEPTqCfQnqlKAXZ0-0pZkmr6omZO3eI1aAis_R1EaZPdMdtxx_28pkplmuPY2vX1oErkbCuZVB
            )

       =&gt; Array
            (
                =&gt; Alert message!
                =&gt; 1
                =&gt; default
                =&gt; Notification
                =&gt; Buy One Pizza Today and Get One Free
            )
    ))
</code></pre>

<hr/>

<p><strong>iOS 代码 -</strong> </p>

<pre><code>import UIKit


@UIApplicationMain

class AppDelegate: UIResponder,UIApplicationDelegate,AsyncDataProviderDelegate,GGLInstanceIDDelegate {

    var window: UIWindow?

    var successToken = &#34;&#34;

    var launchOpt : NSDictionary?

    var rootControllerVC : UIViewController?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {
      UITabBarItem.appearance().setTitleTextAttributes(, forState:.Normal)

      UITabBarItem.appearance().setTitleTextAttributes(, forState:.Selected)


      let tabFont = UIFont(name:&#34;Helvetica-Bold&#34;, size: 12)

      UITabBarItem.appearance().setTitleTextAttributes(, forState:.Normal)


      if #available(iOS 8.0, *)
      {
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])

            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

            application.registerUserNotificationSettings(settings)

            application.registerForRemoteNotifications()
      } else
      {
            application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
      }

      UIApplication.sharedApplication().applicationIconBadgeNumber = 0

      NetworkManager.sharedInstance.delegate = self

      // Override point for customization after application launch.

      return true
    }

    func callRegisterAPi(regId : String)
    {
      let detailDict = NSMutableDictionary()

      detailDict.setObject(kDeviceType, forKey:deviceType)

      detailDict.setObject(kDeviceId, forKey:deviceId)

      detailDict.setObject(regId, forKey: registerID)

      detailDict.setObject(kguiVersion, forKey:guiVersion)

      NetworkManager.sharedInstance.registerDeviceApi(&#34;POST&#34;, isAsynchronous: true, userDictionary: detailDict)
    }


    //MARK:- AsyncData Provider delegate


    func dataGivenBack(resultDataDict: NSMutableDictionary, methodName: String) {
      if successToken != &#34;&#34;
      {
            NSUserDefaults.standardUserDefaults().setObject(successToken, forKey:kToken)
      }
    }


    //MARK:- Push Notification Delegate Methods


    func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

    {
      let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: &#34;&lt;&gt;&#34; )

      let deviceTokenString: String = ( deviceToken.description as NSString ).stringByTrimmingCharactersInSet( characterSet ).stringByReplacingOccurrencesOfString( &#34; &#34;,withString: &#34;&#34; ) as String

      print(&#34;token is \(deviceTokenString)&#34;)

      // Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.

      let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()

      instanceIDConfig.delegate = self

      // Start the GGLInstanceID shared instance with that config and request a registration

      // token to enable reception of notifications

      GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)

      let registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,

            kGGLInstanceIDAPNSServerTypeSandboxOption:true]

      let gcmSenderID = &#34;83985659474&#34;

      GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,

            scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
    }

    func registrationHandler(registrationToken: String!, error: NSError!) {
      if (registrationToken != nil) {
            successToken = registrationToken

            print(&#34;GCM Token is :\(registrationToken)&#34;)

            if isDeviceTokenChanged(registrationToken)
            {
                callRegisterAPi(registrationToken)
            }

            print(&#34;Registred&#34;)
      } else {
            print(&#34;Registration to GCM failed with error: \(error.localizedDescription)&#34;)
      }
    }

    func onTokenRefresh() {
      let registrationOptions = [kGGLInstanceIDRegisterAPNSOption:successToken,

            kGGLInstanceIDAPNSServerTypeSandboxOption:true]

      let gcmSenderID = &#34;83985659474&#34;

      // A rotation of the registration tokens is happening, so the app needs to request a new token.

      print(&#34;The GCM registration token needs to be changed.&#34;)

      GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,

            scope: kGGLInstanceIDScopeGCM, options: registrationOptions as , handler: registrationHandler)
    }

    // Called if unable to register for APNS.

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)

    {
      print(&#34;not registered to APNS,\(error)&#34;)
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: , fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -&gt; Void)

    {
      GCMService.sharedInstance().appDidReceiveMessage(userInfo);

      print(&#34;Receiving dict is :\(userInfo)&#34;)

      rootControllerVC= mainStoryBoard.instantiateViewControllerWithIdentifier(&#34;startingPage&#34;) as UIViewController

      self.window?.rootViewController = rootControllerVC

      UIApplication.sharedApplication().applicationIconBadgeNumber = 0


      completionHandler(UIBackgroundFetchResult.NewData)
    }

    func applicationDidBecomeActive(application: UIApplication) {

      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.


      GCMService.sharedInstance().connectWithHandler({

            (NSError error) -&gt; Void in

            if error != nil {
                print(&#34;Could not connect to GCM: \(error.localizedDescription)&#34;)

            } else
            {
                //                self.connectedToGCM = true

                print(&#34;Connected to GCM&#34;)

                // ...
            }
      })
    }
}
</code></pre>

<blockquote>
<p>{&#34;multicast_id&#34;:8936417917512172262,&#34;success&#34;:1,&#34;failure&#34;:0,&#34;canonical_ids&#34;:0,&#34;results&#34;:[{&#34;message_id&#34;:&#34;0:1452846200241751%bcdb604df9fd7ecd&#34;}]}</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>基于 <a href="https://developers.google.com/cloud-messaging/http-server-ref#error-codes" rel="noreferrer noopener nofollow">Downstream error response codes</a> GCM 部分</p>

<blockquote>
<p>A registration token is tied to a certain group of senders. When a client app registers for GCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration tokens won&#39;t work.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于php - 适用于 iOS 的 Google 云消息传递 MismatchSenderId,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34792791/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34792791/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - 适用于 iOS 的 Google 云消息传递 MismatchSenderId