如何为iOS版本9 + 10(可能还有8个)实施Apple推送通知?

时间:2017-01-23 01:38:32

标签: ios apple-push-notifications multiple-versions

我还没有找到任何正式的Apple文档,该文档讨论了针对旧iOS版本以及iOS 10正确实现推送通知的问题。我所看到的独立教程同样涵盖了单个iOS版本。

我看到iOS 10的官方文档: Local and Remote Notifications Overview但它没有评论支持早期的iOS版本。

iOS 9教程: Push Notifications Tutorial - Ray Wenderlich

我看到各种stackoverflow线程,关于人们为了让旧解决方案在新版本上工作而必须做出的改变:

Push notifications are not working in iOS 9 这显示了处理6 - 9的代码。

didReceiveRemoteNotification not called , iOS 10

但是我没看到的是,从今天开始(使用iOS 10)是正确的,但也支持旧设备。 ** 更新 ** App Store表示只有6%的设备下载应用程序比ios 9旧,所以如果更容易支持9 + 10,我就会这样做。

(我尝试从iOS 10示例开始,但它立即在iOS 9.3仿真设备上崩溃,虽然它在iOS 10中运行良好。所以我得出结论,我应该从正确设置不同版本的信息开始。我可以发布该代码,但我认为这导致了这个线程的方向错误。我宁愿从“应该”在iOS的多个版本上工作开始,包括10个。)

如果我找不到某些解决方案,我将开始合并来自不同stackoverflow代码片段的代码......但是说真的吗?我必须遗漏一些东西,因为大概每个iOS开发人员都有这个问题。

相反,我可以从较旧的示例开始,然后按照更改将其与iOS 10配合使用 - 但是它会充分利用iOS 10吗?

注意:我正在使用Xamarin C#编程,但Objective-C或Swift的答案同样有用。

1 个答案:

答案 0 :(得分:4)

这是Xamarin C#代码(与Objective-C不同的语法和大小写,但我认为它可以逐行转换为Objective-C)。

在iOS 9.3和iOS 10.2上进行测试。

初始化“本地”和“远程”通知:

// "UIApplicationDelegate" is for "local" notifications,
// "IUNUserNotificationCenterDelegate, IMessagingDelegate" for "remote" notifications.
public class AppDelegate : UIApplicationDelegate,
    IUNUserNotificationCenterDelegate, IMessagingDelegate
{
    ...
    public override bool FinishedLaunching( UIApplication application, NSDictionary launchOptions )
    {
        ...
        RegisterForOurRemoteNotifications( this );
        RegisterForOurLocalNotifications();
        ...
    }
    ...

    // --- Comment out if not using Google FCM. ---
    public override void RegisteredForRemoteNotifications( UIApplication application, NSData deviceToken )
    {
        //base.RegisteredForRemoteNotifications( application, deviceToken );
        Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken( deviceToken,
                                                                   Firebase.InstanceID.ApnsTokenType.Sandbox );
    }

    ...
    // ----- "static"; Could be in another class. -----

    // These flags are for our convenience, so we know initialization was done.
    static bool IsRegisteredForNotifications;
    static bool IsRegisteredForRemoteNotifications;
    // Optional - true when we are using Google "Firebase Cloud Messaging".
    static bool HasFCM;

    public static void RegisterForOurRemoteNotifications( AppDelegate del )
    {
        // Google "Firebase Cloud Messaging" (FCM) Monitor token generation
        // (Uncomment, if you are using FCM for notifications.)
        //InstanceId.Notifications.ObserveTokenRefresh( TokenRefreshNotification );

        if (UIDevice.CurrentDevice.CheckSystemVersion( 10, 0 )) {
            // iOS 10 or later
            var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
            UNUserNotificationCenter.Current.RequestAuthorization( authOptions, ( granted, error ) => {
                Console.WriteLine( granted );
            } );

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = del;

            // For iOS 10 data message (sent via Google FCM).
            // (Uncomment, if you are using FCM for notifications.)
            // TBD: If NOT using FCM, you may need some other lines of code here.
            //Messaging.SharedInstance.RemoteMessageDelegate = del;

        } else {
            // iOS 9 or before
            var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
            var settings = UIUserNotificationSettings.GetSettingsForTypes( allNotificationTypes, null );
            UIApplication.SharedApplication.RegisterUserNotificationSettings( settings );
        }

        UIApplication.SharedApplication.RegisterForRemoteNotifications();
        IsRegisteredForRemoteNotifications = true;

        // Uncomment if using Google "Firebase Cloud Messaging" (FCM).
        //TokenRefreshNotification( null, null );
        //if (UIDevice.CurrentDevice.CheckSystemVersion( 9, 0 )) // Needed to call this twice on iOS 9 for some reason.
        //  TokenRefreshNotification( null, null );


        UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval( UIApplication.BackgroundFetchIntervalMinimum );
    }

    public static void RegisterForOurLocalNotifications()
    {            
        // --- Our app's notification actions. ---
        UNNotificationAction followAction = UNNotificationAction.FromIdentifier( "follow", PS.LocalizedString( "Follow" ), UNNotificationActionOptions.None );
        UNNotificationAction likeAction = UNNotificationAction.FromIdentifier( "like", PS.LocalizedString( "Like" ), UNNotificationActionOptions.None );
        // ...

        // --- Our app's notification categories ---
        UNNotificationCategory followCategory = UNNotificationCategory.FromIdentifier( "followCategory", new UNNotificationAction[] { followAction, likeAction },
                                                                                new string[] { }, UNNotificationCategoryOptions.None );
        // ...

        // --- All of the app's categories from above ---
        var categories = new UNNotificationCategory[] { followCategory /*, ...*/ };


        // --- Same for all apps ---
        UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(
                                                  UIUserNotificationType.Alert |
                                                  UIUserNotificationType.Badge |
                                                  UIUserNotificationType.Sound
            , new NSSet( categories ) );
        UIApplication.SharedApplication.RegisterUserNotificationSettings( settings );


        if (UIDevice.CurrentDevice.CheckSystemVersion( 10, 0 )) {
            UNUserNotificationCenter.Current.SetNotificationCategories( new NSSet<UNNotificationCategory>( categories ) );

            UNUserNotificationCenter.Current.RequestAuthorization( UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Badge,
                                                                  ( result, err ) => {
                                                                      Console.WriteLine( result.ToString() );
                                                                  } );
        }

        IsRegisteredForNotifications = true;
    }
}


    // -------------------------------------------------------
    // --- These are for Google "Firebase Cloud Messaging" ---
    // (Comment out if not using FCM.)

    public static string Token;

    static void TokenRefreshNotification( object sender, NSNotificationEventArgs e )
    {
        // This method will be fired every time a new token is generated, including the first
        // time. So if you need to retrieve the token as soon as it is available this is where that
        // should be done.
        //var refreshedToken = InstanceId.SharedInstance.Token;

        ConnectToFCM( UIApplication.SharedApplication.KeyWindow.RootViewController );

        // TODO: If necessary send token to application server.
    }


    public static void ConnectToFCM( UIViewController fromViewController )
    {
        Messaging.SharedInstance.Connect( error => {
            if (error != null) {
                Helper.logD( "Unable to connect to FCM", error.LocalizedDescription );
            } else {
                //var options = new NSDictionary();
                //options.SetValueForKey( DeviceToken, Constants.RegisterAPNSOption );
                //options.SetValueForKey( new NSNumber( true ), Constants.APNSServerTypeSandboxOption );

                //InstanceId.SharedInstance.GetToken("", InstanceId.ScopeFirebaseMessaging 
                Token = InstanceId.SharedInstance.Token;

                Console.WriteLine( $"Token: {InstanceId.SharedInstance.Token}" );
                HasFCM = true;
            }
        } );
    }
    // ------------------ End Google FCM ---------------------
    // -------------------------------------------------------
}

以上代码初始化您的应用,以便它可以接收通知。

重要提示:您还需要在应用中设置适当的权限;请参阅Apple文档或问题中提到的链接。你需要这个文件:

Entitlements.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>
上面的

<string>必须包含“开发”或“生产”。 (我不知道我们的应用仍然在这里说“开发”的重要性;我还没有检查是什么构建的,看它是否在提交给Apple之前被Xcode自动更改为“生产”。根据{{3}它确实。)

然后您需要代码发送 [例如您的应用告诉您的服务器通知您朋友的设备您现在正在做什么]并接收本地或远程通知。在我们的应用中,该代码与我们的特定通知操作和类别相结合;我没有时间提取一个简洁的版本发布在这里。有关详细信息,请参阅Apple文档或原始问题中提到的链接。

以下是接收通知的基本方法(上面添加到class AppDelegate):

    public override void ReceivedLocalNotification( UIApplication application, UILocalNotification notification )
    {
        ...
    }

    public override void DidReceiveRemoteNotification( UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler )
    {
        ...
    }


    [Export( "userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:" )]
    public void DidReceiveNotificationResponse( UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler )
    {
        ...
    }

您可能需要/需要覆盖或实现的其他方法(另请参阅上面class AppDelegate上声明的接口);其中一些可能是FCM特有的:

ApplicationReceivedRemoteMessage
ReceivedRemoteNotification
WillPresentNotification
PerformFetch (for background notifications)
HandleAction