Sample written in Objective using Swift SDK for IBM Cloud push service
- Xcode 10+
- Swift 4.+
- push service
Create a push service and configure for iOS notification - follow this doc
- Create a pod file by running
pod init
in your projects root folder - run
pod install
- open the
projectName.xcworkspace
in Xcode - Create a
.swift
file namedBMSPushBinder
. This will create aBridging-Header
file too. - Copy paste the code from
HelloPush/BMSPushBinder.swift
to yourBMSPushBinder.swift
file - Once the swift class is added , you will be able to access the push functions from any objective class.
- Dont forget to add
#import "HelloPush-Swift.h"
and@class BMSPushBinder;
in the.h
header files.
Initialize push service with options (category, deviceId and parameters)
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
BMSPushNotificationAction *actionOne = [bmsPushBinder createActionWithIdentifierName:@"FIRST" buttonTitle:@"Accept" isAuthenticationRequired:FALSE defineActivationMode: UIUserNotificationActivationModeForeground];
BMSPushNotificationAction *actionTwo = [bmsPushBinder createActionWithIdentifierName:@"actionTwo" buttonTitle:@"Reject" isAuthenticationRequired:FALSE defineActivationMode: UIUserNotificationActivationModeBackground];
BMSPushNotificationActionCategory *category = [bmsPushBinder createNotificationActionCategoryWithIdentifierName:@"category" buttonActions:@[actionOne, actionTwo]];
BMSPushClientOptions *notificationOptions = [bmsPushBinder createOptionsWithDeviceId:@"mydeviceID" categories:@[category] parameters:nil];
NSString * appGuid = @"push service appguid";
NSString * clientSecret = @"push service clientSecret";
NSString * bluemixRegion = [BMSPushBinder regionUsSouth];
[bmsPushBinder initializeWithAppGUIDWithAppGuid:appGuid clientSecret:clientSecret bluemixRegion: bluemixRegion options:notificationOptions];
Initialize push service without category
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
NSString * appGuid = @"push service appguid";
NSString * clientSecret = @"push service clientSecret";
NSString * bluemixRegion = [BMSPushBinder regionUsSouth];
[bmsPushBinder initializeWithAppGUIDWithAppGuid:appGuid clientSecret:clientSecret bluemixRegion: bluemixRegion options:nil];
The bluemixRegion
can be one of these ,
- regionUsSouth
- regionUnitedKingdom
- regionSydney
- regionGermany
- regionUsEast
- regionJpTok
To register for push notification, add the following code inside the application:didRegisterForRemoteNotificationsWithDeviceToken:
method of AppDelegate.m
file,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
[bmsPushBinder registerForPushWithDeviceToken:deviceToken userId:@"myUserId" completionHandler:^(NSString * response, NSNumber * statusCode, NSString * error) {
if (error != nil) {
NSLog(@"Registration Error, %@", error);
} else {
NSLog(@"Registration Success , %@", response);
}
}];
}
To un-register from the push service ,
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
[bmsPushBinder unregisterFromPushWithCompletionHandler:^(NSString * response, NSNumber * statusCode, NSString * error) {
if(error != nil) {
NSLog(@"Error : %@", error);
} else {
NSLog(@"Unregistred: %@", response.description);
}
}];
To retrieve available tags use the following ,
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
[bmsPushBinder retrieveAvailableTagsWithCompletionHandler:^(NSMutableArray * response , NSNumber * statusCode, NSString * error) {
if(error != nil) {
NSLog(@"Error : %@", error);
} else {
NSLog(@"Available tags: %@", response.description);
}
}];
To subscribe to tags ,
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
[bmsPushBinder subscribeToTagsWithTagsArray:@[@"tag1"] completionHandler:^(NSMutableDictionary * response, NSNumber * statusCode, NSString * error) {
if(error != nil) {
NSLog(@"Error : %@", error);
} else {
NSLog(@"subscribed: %@", response.description);
}
}];
To Un-Subscribe from tag ,
BMSPushBinder *bmsPushBinder = [BMSPushBinder sharedInstance];
[bmsPushBinder unsubscribeFromTagsWithTagsArray:@[@"tag1"] completionHandler:^(NSMutableDictionary * response, NSNumber * statusCode, NSString * error) {
if(error != nil) {
NSLog(@"Error : %@", error);
} else {
NSLog(@"unsubscribed from tags : %@", response.description);
}
}];
-
Please visit for samples - Github Sample
-
Video Tutorials Available here - IBM Cloud Push Notifications
Twitter | YouTube | Blog | Facebook | Meetup
======================= Copyright 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.