Multiple push providers

Handle multiple push providers in mobile applications

If you have multiple push providers in your mobile application, you have to slightly alter the integration to remove duplicate instances of dependency classes, and to appropriately handle incoming messages.

Android

This guide is for Android apps using version 7.2.0 and greater of the Xtremepush SDK.

Add the following to your manifest file to remove the Xtremepush FirebaseMessageingService child class entry. This will prevent any potential conflict between the two push providers.

<service
    android:name="ie.imobile.extremepush.google.XPFirebaseMessagingService"
    tools:node="remove">
</service>

Now, inside your own FirebaseMessagingService child class you will need to update the onMessageReceived function so that it handles incoming messages as follows:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    //Check if message is xpush message
    if (PushConnector.isXpushMessage(this, remoteMessage)) {
        //if xpush message, pass to xpush to manage
        PushConnector.fcmShareRemoteMessage(this, remoteMessage);
    } else {
        //handle non xpush message
    }

}

In the same class, you will also need to update the onNewToken function, as follows:

@Override
public void onNewToken(String token) {
    //share the token with XPush so it is available to both providers
    PushConnector.fcmShareToken(this, token);
    //non xpush use of token
}

iOS

if you need to check if a message has come from Xtremepush, so you can ignore it, you could do the following:

- (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo {
    if ([userInfo objectForKey:@"xpush"] != nil) {
        [XPush applicationDidReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
    }
    // Do you our own thing if needed
}

// Called when a notification is received while the app is in the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    NSDictionary *userInfo = notification.request.content.userInfo;

    if (userInfo[@"xpush"] != nil) {
        // Forward XPush notifications to the SDK
        [XPush userNotificationCenter:center
              willPresentNotification:notification
                withCompletionHandler:completionHandler];
        return; // SDK calls completionHandler
    }

    // Handle non-XPush notifications here (if applicable)
    completionHandler(UNNotificationPresentationOptionAlert);
}


// Called when the user taps a notification or interacts with an action button
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {

    NSDictionary *userInfo = response.notification.request.content.userInfo;

    if (userInfo[@"xpush"] != nil) {
        // Forward XPush notifications to the SDK
        [XPush userNotificationCenter:center
     didReceiveNotificationResponse:response
              withCompletionHandler:completionHandler];
        return; // SDK calls completionHandler
    }

    // Handle non-XPush notification actions here (if applicable)
    completionHandler();
}

// Called when the user taps a notification or interacts with an action button
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {

    NSDictionary *userInfo = response.notification.request.content.userInfo;

    if (userInfo[@"xpush"] != nil) {
        // Forward XPush notifications to the SDK
        [XPush userNotificationCenter:center
     didReceiveNotificationResponse:response
              withCompletionHandler:completionHandler];
        return; // SDK calls completionHandler
    }

    // Handle non-XPush notification actions here (if applicable)
    completionHandler();
}
func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    if userInfo["xpush"] != nil {
        XPush.applicationDidReceiveRemoteNotification(userInfo, fetchCompletionHandler: nil)
    }
    // Do you our own thing if needed    
}

// Called when a notification is received while the app is in the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    NSDictionary *userInfo = notification.request.content.userInfo;

    if (userInfo[@"xpush"] != nil) {
        // Forward XPush notifications to the SDK
        [XPush userNotificationCenter:center
              willPresentNotification:notification
                withCompletionHandler:completionHandler];
        return; // SDK calls completionHandler
    }

    // Handle non-XPush notifications here (if applicable)
    completionHandler(UNNotificationPresentationOptionAlert);
}

// Called when the user taps a notification or interacts with an action button
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {

    NSDictionary *userInfo = response.notification.request.content.userInfo;

    if (userInfo[@"xpush"] != nil) {
        // Forward XPush notifications to the SDK
        [XPush userNotificationCenter:center
     didReceiveNotificationResponse:response
              withCompletionHandler:completionHandler];
        return; // SDK calls completionHandler
    }

    // Handle non-XPush notification actions here (if applicable)
    completionHandler();
}

If using the Xtremepush Phonegap plugin for iOS please follow the additional Phonegap Multiple Push Providers iOS