How to get registrationId from Azure notificationhub in iOS - ios

I need registrationId from Azure. Is it possible to get the registrationId from Azure?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString:HUBLISTENACCESS
notificationHubPath:HUBNAME];
[hub registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError* error) {
if (error != nil) {
NSLog(#"Error registering for notifications: %#", error);
}
else {
[self MessageBox:#"Registration Status" message:#"Registered"];
}
}];
}
Notifications are working fine but I need registrationId from Azure to send back to server.

I pored through the NotificationHub SDK code available at https://github.com/Azure/azure-notificationhubs/tree/master/iOS/WindowsAzureMessaging/WindowsAzureMessaging and found that the registration methods are all internal to the SDK class SBNotificationHub and are not exposed via the ObjC header file. Until they do that you either override the SDK header file with your own or accept that you can't access the registrationId from the SDK. The methods in question are retrieveAllRegistrationsWithDeviceToken or extractRegistrationIdFromLocationUri. The latter also requires that you call composeCreateRegistrationIdUri and then registrationOperationWithRequestUri.

Related

Firebase iOS Messaging, didReceiveMessage, appData is deprecated

I am getting appData is deprecated below with the new Firebase project. What else should I use for data messages only?
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
NSLog(#"Received data message: %#", remoteMessage.appData);
if(remoteMessage.appData[#"action"] != nil)
{
if([#"refresh" isEqual: remoteMessage.appData[#"action"]])
{
[[FIRInstanceID instanceID]deleteIDWithHandler:^(NSError * _Nullable error) {
NSLog(#"%#", error.description);
if(error == nil) {
}
}];
}
}
}
From https://firebase.google.com/support/release-notes/ios:
Deprecated FCM direct channel messaging via shouldEstablishDirectChannel. Instead, use APNs for downstream message delivery. Add content_available key to your payload if you want to continue use legacy APIs, but we strongly recommend HTTP v1 API as it provides full APNs support.

Another way to get push device token ios?

I wonder, is there another way to get the push device token without the delegate method:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
because I used OneSignal, and they don't need to conform that method to get the device token.
You can get the push token for OneSignal SDK like this:
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
if let deviceState = OneSignal.getDeviceState() {
let userId = deviceState.userId
let pushToken = deviceState.pushToken
let subscribed = deviceState.isSubscribed
print("pushToken ====> \(pushToken)") //Here you can get the token
}
})
Keep coding........... :)
OneSignal API states IdsAvailable is the method you would use:
[OneSignal IdsAvailable:^(NSString* userId, NSString* pushToken) {
NSLog(#"UserId:%#", userId);
if (pushToken != nil)
NSLog(#"pushToken:%#", pushToken);
}];
Is the method to get the token; mentioned here.

OneSignal Not calling didReceiveRemoteNotification

We migrated from UA to One Signal. We are sending push from cloud code like
var pushInfo = {
"app_id" : "xxxxxx",
"data": {
"objectId": objectId,
"placeId": placeId,
},
"included_segments": ["All Users"],
"contents": {"en": message}
};
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic XXXXX"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers,
};
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("Response:");
console.log(JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(pushInfo));
req.end();
In my AppDelegate.m I do
[OneSignal initWithLaunchOptions:launchOptions appId:#"XXXXX"];
Now earlier when a notification is received and user Taps on it, it used to call
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
Q. This is not getting called now. How do I handle it with OneSignal.
Q. What I need to do to handle a silent notification (no visible badge/banner etc)
I Assume you are testing/running your app on an iOS10 device,
I looked at OneSignal SDK Code and I think the SDK automatically uses the new UserNotifications Framework (add in iOS10), when iOS10 is detected on device.
In this case, the AppDelegate method you mentioned above does not get invoked, instead methods in UNUserNotificationCenterDelegate get invoked, which are captured by SDK to record clicks/views.
To Fix the issue, Create a new class which implements OSUserNotificationCenterDelegate and provide its instance to OneSignal using [OneSignal setNotificationCenterDelegate:yourCustomNotificationCenterDelegateInstance]
Please note that application:didReceiveRemoteNotification:fetchCompletionHandler: is still called when a silent push notification (content-available: 1) arrives, but its not called when user taps the notification if UNUserNotificationCenterDelegate is used.
Also, there was an issue on iOS 10.0.X where the application:didReceiveRemoteNotification was called instead of application:didReceiveRemoteNotification:fetchCompletionHandler: See: https://forums.developer.apple.com/thread/54332 , but I doubt if this is the case with you.
One Signal Notification Integration
Use Following Block Of Code TO Handle PushNotification Message Content
Put below code in AppDelegate's ApplicationDidFinishLaunch Options method
let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
print("Received Notification: \(notification!.payload.notificationID)")
}
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
// This block gets called when the user reacts to a notification received
let payload: OSNotificationPayload = result!.notification.payload
var fullMessage = payload.body
print("Message = \(String(describing: fullMessage))")
if payload.additionalData != nil {
if payload.title != nil {
let messageTitle = payload.title
print("payload.category \(payload.category)")
print("payload.subtitle \(payload.subtitle)")
print("Message Title = \(messageTitle!)")
}
let additionalData = payload.additionalData
if additionalData?["actionSelected"] != nil {
fullMessage = fullMessage! + "\nPressed ButtonID: \(String(describing: additionalData!["actionSelected"]))"
}
}
}
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
kOSSettingsKeyInAppLaunchURL: true]
OneSignal.initWithLaunchOptions(launchOptions,
appId: "Your App Id",
handleNotificationReceived: notificationReceivedBlock,
handleNotificationAction: notificationOpenedBlock,
settings: onesignalInitSettings)
**Use following block of code To Receive OneSingnal Notification Content**
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
// Recommend moving the below line to prompt for push after informing the user about
// how your app will use them.
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
application:didReceiveRemoteNotification:fetchCompletionHandler: is the correct selector for background silent content-available notifications. Make sure you are using the latest 2.2.2 OneSignal SDK as there were some fixes to maintain compatibility with the older AppDelegate selectors.
You may want to look into using UNNotificationServiceExtension with mutable-content for iOS 10 devices as this still works when the app has been swiped away.
In my case all I had to do is remove the Firebase handlers in app delegate. OneSignal can hijack the events from FCM. This way I had both the OneSingal Push notification and the ones delivered by Firebase. As for One signal I used a simple copy and paste from their examples.
THIS ARE THE PARTS THAT I REMOVED FROM MY AppDelegate.m
REMOVE IMPORTS -> #import "RNFirebaseMessaging.h"
REMOVE IMPORTS -> #import "RNFirebaseNotifications.h"
IN: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
REMOVE ->[RNFirebaseNotifications configure];
REMOVE HANDLERS:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[[RNFirebaseNotifications instance]
didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^).
(UIBackgroundFetchResult))completionHandler{
[[RNFirebaseNotifications instance]
didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler];
[UIApplication sharedApplication].applicationIconBadgeNumber += 1;
}
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings
*)notificationSettings {
[[RNFirebaseMessaging instance]
didRegisterUserNotificationSettings:notificationSettings];
}
import { Platform } from 'react-native'
import OneSignal, { NotificationReceivedEvent, OpenedEvent } from 'react-native-onesignal'
import Config from '../../Config/Config'
import State from '../../State/State'
interface WithSuccess {
success: boolean
}
interface ExternalUserIdResultI {
push: WithSuccess
email: WithSuccess
sms: WithSuccess
}
class Messaging {
public Init = () => {
//OneSignal Init Code
OneSignal.setLogLevel(Config.messaging.debugLevel, 0)
OneSignal.setAppId(Platform.OS === 'ios' ? Config.messaging.iosAppId : Config.messaging.androidAppId)
//END OneSignal Init Code
//Prompt for push on iOS
OneSignal.promptForPushNotificationsWithUserResponse((response: boolean) => {
console.log("Prompt response:", response);
})
//Method for handling notifications received while app in foreground
OneSignal.setNotificationWillShowInForegroundHandler((notificationReceivedEvent: NotificationReceivedEvent) => {
console.log("OneSignal: notification will show in foreground:", notificationReceivedEvent);
let notification = notificationReceivedEvent.getNotification();
console.log("notification: ", notification);
notificationReceivedEvent.complete(notification)
})
//Method for handling notifications opened
OneSignal.setNotificationOpenedHandler((notification: OpenedEvent) => {
console.log("OneSignal: notification opened:", notification);
})
OneSignal.addSubscriptionObserver(event => {
console.log("OneSignal: subscription changed:", event);
})
}
public SendTag = (key: string, value: string) => {
OneSignal.sendTag(key, value)
}
public SetExternalUserId = (externalUserId: string) => {
//#ts-ignore
OneSignal.setExternalUserId(externalUserId, (results: ExternalUserIdResultI) => {
// The results will contain push and email success statuses
console.log('Results of setting external user id');
console.log(results);
// Push can be expected in almost every situation with a success status, but
// as a pre-caution its good to verify it exists
if (results.push && results.push.success) {
console.log('Results of setting external user id push status:');
console.log(results.push.success);
}
// Verify the email is set or check that the results have an email success status
if (results.email && results.email.success) {
console.log('Results of setting external user id email status:');
console.log(results.email.success);
}
// Verify the number is set or check that the results have an sms success status
if (results.sms && results.sms.success) {
console.log('Results of setting external user id sms status:');
console.log(results.sms.success);
}
});
}
public RemoveExternalUserId = () => {
//#ts-ignore
OneSignal.removeExternalUserId((results: ExternalUserIdResultI) => {
// The results will contain push and email success statuses
console.log('Results of removing external user id');
console.log(results);
// Push can be expected in almost every situation with a success status, but
// as a pre-caution its good to verify it exists
if (results.push && results.push.success) {
console.log('Results of removing external user id push status:');
console.log(results.push.success);
}
// Verify the email is set or check that the results have an email success status
if (results.email && results.email.success) {
console.log('Results of removoing external user id email status:');
console.log(results.email.success);
}
});
}
}
export default new Messaging()

Easy way to unsubscribe for all topic subscribed with GCM (iOS device)

I am trying to push notifications through topic system in a iOS device with the new API of Google Cloud Messaging designed for iOS device.
I have the right certificates so I can receive notifications from a topic created. My code to subscribe to a topic is the following :
if (_registrationToken && _connectedToGCM) {
[[GCMPubSub sharedInstance] subscribeWithToken:_registrationToken
topic:topicToSubscribe
options:nil
handler:^(NSError *error) {
if (error) {
//handle error here
} else {
self.subscribedToTopic = true;
}
}];
}
I know the equivalent function to unsubscribe but this function need a topic name.
Is there a way to retrieve all topics where my app is possibly subscribed to unregistered them before subscribing ?
There is no way to retrieve the list of topics that your app is subscribed to from the Google Cloud Messaging service.
You have to keep track of the list and persist it on your app (hard coded, stored in preferences, database, file, etc.) or your server.
When you decide to let the user unsubscribe, retrieve the list of topics from where you stored it and pass it to unsubscribeWithToken:token:topic:options:handler as mentioned on the Implementing Topic Messaging page
Alternatively, when receiving messages you can check who is the message 'from'. If it is from a topic you are no longer interested in, you can unsubscribe instead of processing the message.
If you have the registration token it's possible to get the topics the device is subscribed to by using https://iid.googleapis.com/iid/info/IID_TOKEN (with authorization key in the header). Where IID_TOKEN is the registration token.
Find more info at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances.
If you want unsubscribe from all topics simply execute:
GGLInstanceID *iid = [GGLInstanceID sharedInstance];
GGLInstanceIDDeleteHandler deleteHandler = ^void(NSError *error) {
if (error) {
// failed to delete the identity for the app
// do an exponential backoff and retry again.
} else {
// try to get a new ID
dispatch_async(dispatch_get_main_queue(), ^{
GGLInstanceIDHandler handler =
^void(NSString *identity, NSError *error) {
if (error) {
// failed to get the identity for the app
// handle error
} else {
NSString *instanceID = identity;
// handle InstanceID for the app
}
}
[iid getIDWithHandler:handler];
});
}
}
[iid deleteIDWithHandler:deleteHandler];
More info
Don't forget to refresh your TOKEN!

Retrieve User email using GTM OAuth2 for iOS

I have successfully implemented authentication using GTM OAuth 2 library. But now I want to have the email id of the user. How should I proceed. I know I have to call something in here :-
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error != nil) {
NSLog(#"SIGN IN ERROR : %#", error.description);
// Authentication failed
} else {
// Authentication succeeded
}
}
When signing in to Google services with gtm-oauth2, the user's email address is available after sign-in in the auth object's userEmail property.

Resources