Requirements:
- Create a Pushwoosh account if you do not already have one.
- Real device with iOS 9 and later. The Xcode simulator doesn't support push notifications so you must test on a real device.
- A Mac with a new version of Xcode.
- An iOS Push Certificate. Details see: iOS configuration
Integration:
You can also use Carthage for setting up and upgrading the Pushwoosh SDK.
1. Open project directory in terminal. Run the following command:
echo 'github "Pushwoosh/pushwoosh-ios-sdk"' >> Cartfile
2. Run the following command:
carthage update --use-xcframeworks --platform iOS
Make sure that your Carthage version is 0.38.0.
3. Open your Xcode project.
4. Go to your application targets "General" settings tab, "Frameworks, Libraries, and Embedded Content" section.
5. Drag and drop Pushwoosh.xcframework from <Your Project Directory>/Carthage/Build folder on disk.
If an error occurs, use a workaround – setting Validate Workspace to Yes in the Build Settings tab
6. That's it!
Add Required Capabilities:
- Select the root project (1), your main app target (2), and the "Signing and Capabilities" tab.
- Press "+ Capability" button (3) and select "Push Notifications" capability.
- Then, add the "Background Modes" capability and check the "Remote notifications" check box (4).
4. Well done! Xcode capabilities configuration completed.
XCODE 13 Time Sensitive notifications:
If you want to send Time Sensitive notifications add Time Sensitive notifications capability (read more about time sensitive push notifications here).
Add iOS Message Delivery Tracking:
There is an API method in Pushwoosh that tracks the delivery of push notifications. iOS apps do not support this method out of the box as push notifications in iOS are handled by the OS, not by Pushwoosh SDK. However, you can implement delivery tracking by adding the Pushwoosh Notification Service Extension for push delivery tracking to your project. Here you'll find the steps to implement Message Delivery Tracking for iOS apps.
Available on iOS 10.0 and later
1. Add Notification Service Extension
1.1. In Xcode, Select File > New > Target...
1.2. Select Notification Service Extension and press Next.
1.3 Enter the product name and press Finish.
Do not select Activate on the dialog that is shown after pressing Finish.
Press Cancel on the Activate scheme prompt.
By canceling, you are keeping Xcode debugging your app, instead of the extension you just created. If you activated it by accident, you can switch back to debug your app within Xcode.
2. Add code for tracking message delivery events
2.1 Add the following code to your NotificationService.m file:
Swift code:
import UserNotifications
import PushwooshFramework
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// Pushwoosh **********
PWNotificationExtensionManager.shared().handle(request, contentHandler: contentHandler)
// ********************
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
Objective-C code:
#import "PWNotificationExtensionManager.h"
@interface NotificationService : UNNotificationServiceExtension
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
// Pushwoosh **********
[[PWNotificationExtensionManager sharedManager] handleNotificationRequest:request contentHandler:contentHandler];
//*********************
}
@end
2.2 Add Pushwoosh_APPID to your Notification Service Extension info.plist.
<key>Pushwoosh_APPID</key>
<string>XXXXX-XXXXX</string>
2.3 Add your API key from the Pushwoosh control panel to the Info.plist file using the key "PW_API_TOKEN" with the data type set to "String".
3. Add the App Groups capability for each target of your application:
4. Add the App Groups ID:
4.1 Add the App Groups ID to your info.plist for each target of your application:
<key>PW_APP_GROUPS_NAME</key>
<string>group.com.example.demoapp_example</string>
4.2 If you do not want to use the info.plist file, use the method below and add the code to your NotificationServiceExtension class:
Swift code:
PWNotificationExtensionManager.shared().handle(request, withAppGroups: "group.com.example.demoapp_example")
Objective-C code:
[[PWNotificationExtensionManager sharedManager] handleNotificationRequest:request withAppGroups:@"group.com.example.demoapp_example"];
Add the Pushwoosh Initialization code:
1. Add the following code to your AppDelegate class:
Swift code:
import PushwooshFramework
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PWMessagingDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Initialization code
// Set custom delegate for push handling, in our case AppDelegate
Pushwoosh.sharedInstance()?.delegate = self
// Register for push notifications
Pushwoosh.sharedInstance()?.registerForPushNotifications()
return true
}
// Handle token received from APNS
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Pushwoosh.sharedInstance()?.handlePushRegistration(deviceToken)
}
// Handle token receiving error
func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error
) {
Pushwoosh.sharedInstance()?.handlePushRegistrationFailure(error)
}
// This is for iOS < 10 and for silent push notifications
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
Pushwoosh.sharedInstance()?.handlePushReceived(userInfo)
completionHandler(.noData)
}
// This event is fired when the push gets received
func pushwoosh(
_ pushwoosh: Pushwoosh!,
onMessageReceived message: PWMessage!
) {
print("onMessageReceived: ", message.payload.description)
}
// This event is fired when a user taps the notification
func pushwoosh(
_ pushwoosh: Pushwoosh!,
onMessageOpened message: PWMessage!
) {
print("onMessageOpened: ", message.payload.description)
}
}
Objective-C:
#import <PushwooshFramework/PushwooshFramework.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//-----------PUSHWOOSH PART-----------
// set custom delegate for push handling, in our case AppDelegate
[Pushwoosh sharedInstance].delegate = self;
//register for push notifications
[[Pushwoosh sharedInstance] registerForPushNotifications];
return YES;
}
//handle token received from APNS
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[Pushwoosh sharedInstance] handlePushRegistration:deviceToken];
}
//handle token receiving error
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[Pushwoosh sharedInstance] handlePushRegistrationFailure:error];
}
//this is for iOS < 10 and for silent push notifications
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[Pushwoosh sharedInstance] handlePushReceived:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}
//this event is fired when the push gets received
- (void)pushwoosh:(Pushwoosh *)pushwoosh onMessageReceived:(PWMessage *)message {
NSLog(@"onMessageReceived: %@", message.payload);
}
//this event is fired when a user taps the notification
- (void)pushwoosh:(Pushwoosh *)pushwoosh onMessageOpened:(PWMessage *)message {
NSLog(@"onMessageOpened: %@", message.payload);
}
@end
2. In your Info.plist, add a string type key Pushwoosh_APPID
with your Pushwoosh Application Code as a value.
3. Add your API key from the Pushwoosh control panel to the Info.plist file using the key "PW_API_TOKEN" with the data type set to "String".
Now you can send your first push notification!
Testing push notifications on test devices:
If you want to test push notifications on test devices without affecting the production environment, follow the steps below. If you do not need to use this functionality, skip this section.
1. Add pushwoosh-YOUR_PUSHWOOSH_APP_ID URL scheme to the Info.plist file:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.pushwoosh.scheme</string>
<key>CFBundleURLSchemes</key>
<array>
<string>pushwoosh-YOUR_PUSHWOOSH_APP_ID</string>
</array>
</dict>
</array>
Don't forget to replace YOUR_PUSHWOOSH_APP_ID in the XML above with your Pushwoosh App ID. Example: pushwoosh-ABCDE-EDCBA
2. Launch the application to subscribe for push notifications.
3. Scan the QR Code in the Test Devices form using any appropriate QR Code scanner.
Comments
0 comments
Please sign in to leave a comment.