Contact Us

Installation

Pendo Help CenterInstallationMobile Rich Push Notifications

Mobile Rich Push Notifications

Send rich push notifications via Pendo [Android]

To enable Rich Push notification guides follow the instructions below.

note: Before you complete the appropriate configuration steps in Pendo UI, be sure that your Android app is properly configured for push notifications. Consult Android documentation for more details.

After ensuring your app is enabled to receive push notifications, you need to complete two steps that are specific to Pendo’s SDK. First, you need to provide your Server Key so that Pendo can send push notifications to the app.

APIKEY-Android.png

To provide the Server key, navigate to the Pendo Admin page and then click on your app in the list. Next, click the App Details link in the menu on the top. You will see a section called Push Settings. Click the edit icon, and then set your API key and optionally the name of an icon image and sound file in your app.

android_push.png

Second, you need to add a small amount of code to your app. Instructions differ based on whether you are using Firebase Cloud Messaging.

Firebase Instructions

Use the google instructions in here to configure Firebase. Google also provides a sample here. Step 1: in your main Activity Add the following code to your onCreate() method:

Insert.setPushId(FirebaseInstanceId.getInstance().getToken());

Step 2: Create FirebaseMessagingService class:

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import sdk.insert.io.Insert;
import sdk.insert.io.InsertPushHandler;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
   @Override
   public void onMessageReceived(RemoteMessage remoteMessage) {
       if (Insert.isInsertPush(remoteMessage.getData())) {
           InsertPushHandler.handleInsertPush(remoteMessage.getData());
       }
   }
}

Step 3: Create FirebaseInstanceIdService class:

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

import sdk.insert.io.Insert;

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
   @Override
   public void onTokenRefresh() {
       super.onTokenRefresh();
       Insert.setPushId(FirebaseInstanceId.getInstance().getToken());
   }
}

Firebase knows to start this service whenever a new token is generated so you do not need to do any additional work.

Step 4: Android Manifest: Add under tag;

     <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service android:name=".MyFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

Rich Push Notification [iOS]

Follow the steps below to use Pendo to send and receive push notifications in your app. Before you complete the appropriate configuration steps in Pendo, be sure that your iOS app is properly configured for push notifications. There are many good articles that describe how to implement push notifications in iOS, and you can also consult Apple documentation. After ensuring your app is enabled to receive push notifications, you need to complete two steps that are specific to Pendo. First, you need to provide your APNS certificate in P12 format so that Pendo can send push notifications to the app. To provide the P12 certificate, navigate to the [Subscription Settings page] (https://app.pendo.io/admin) and then click on your app in the list. Next, click the App Details link in the menu on the top. You will see a section called Push Settings. Click the edit icon, and then upload the appropriate P12 certificate and specify the password you used when creating the certificate.

push_settings_ios.png

Note that you must upload either the Development or Production certificate to use Pendo push notifications. You may upload both. Once you provide a valid P12 certificate, you need to add two lines of code to your app’s source:

Second, you need to add two lines of code to your app source. Push registration When registering to receive push notifications, you must pass the push notification token to the Pendo service. You do this by setting the pushId property on the InsertManager instance in the application:didRegisterForRemoteNotificationsWithDeviceToken: function of your AppDelegate:

(Objective-C): “`C++ [InsertManager sharedManager].pushId =

**(Swift):**
```Swift
InsertManager.sharedManager().pushId = <device token>

Notification forwarding

When your app receives the push notification, you must pass the notification object to Pendo. You do this by adding the following code in the application:didReceiveRemoteNotification: function of your AppDelegate class:

(Objective-C): “`C++ [[InsertManager sharedManager] didReceiveRemoteNotification:userInfo];

**(Swift):**
```Swift
InsertManager.sharedManager().didReceiveRemoteNotification(userInfo: userInfo)

Create a notification extension

A notification extension provides developers a mechanism to intercept a push notification before it is delivered to the user. This allows for special handling of payload data, such as managing media included in a push notification. The example below creates a notification extension which extracts the image provided as part of the guide definition and shows it as part of the notification.

  • Under the Xcode menu, use File | New | Target and choose Notification Service Extension
  • Give the extension a name. Verify that the new code inherits from UNNotificationServiceExtension
  • Override the code with your own implementation. Below is an example.
(void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    // checking if push notification originated from Pendo
    // Push notification will contain a flag "insertData"
    if (request.content.userInfo[@"insertData"]) {
        NSLog(@"Received a remote notification from insert: %@",request.content.userInfo);
        // get image URL key from push notification
        NSString *imageUrl = request.content.userInfo[@"image_url"];
        NSURL *attachmentURL = [NSURL URLWithString:imageUrl];
        if (!attachmentURL) {
            // not a rich push notification. We will display the default push notification UI
            contentHandler(request.content);
            return;
        }
        // Download the image URL
        self.downloadTask = [[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] downloadTaskWithURL:attachmentURL completionHandler:^(NSURL * _Nullable fileLocation, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // error handling
            if (error) {
                NSLog(@"Error downloading content %@. Error Message: %@",attachmentURL,error);
                // present the default push UI
                contentHandler(request.content);
                return ;
            }
            // Move the attachment to a readable location
            NSFileManager *fileManager = [NSFileManager defaultManager];
            // take the name of the file
            NSString *fileSuffix = attachmentURL.lastPathComponent;
            NSURL *typedAttachmentURL = [NSURL fileURLWithPath:[(NSString *_Nonnull)fileLocation.path stringByAppendingString:fileSuffix]];
            [fileManager moveItemAtURL:fileLocation toURL:typedAttachmentURL error:&error];
            NSError *attachError = nil;
            // create the image attachment for the push UI
            UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"insert" URL:typedAttachmentURL options:nil error:&attachError];
            if (attachError) {
                NSLog(@"Error creating the attachment %@",attachError);
            }
            if (attachment == nil) {
                contentHandler(request.content); //Nothing to add to the push, return early.
                return;
            }
            // present the rich notification with the image attachment
            UNMutableNotificationContent *modifiedContent = request.content.mutableCopy;
            [modifiedContent setAttachments:[NSArray arrayWithObject:attachment]];
            contentHandler(modifiedContent);
        }];
        [self.downloadTask resume];
    }
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
}

Using Cordova with Push Notification

  1. Create a new Cordova Project or use an existing project.
  2. Add the Pendo plug-in as discussed in here
  3. Add the Push notification cordova plug in using the following command line:

cordova plugin add phonegap-plugin-push --variable SENDER_ID=<sender-id>

Add the following code after "onDeviceReady" in your javascript file

var push = PushNotification.init({ "android": {"senderID": "<sender-id>"},
 "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
    push.on('registration', function(data) {
        window.plugins.Insert.setPushId(data.registrationId);
   });
    push.on('notification', function(data) {
       // this function will be called when the push notification is received by the app
   });
    push.on('error', function(e) {
       console.log(e.message);
   });
© 2018 Pendo  |  Terms of Service  |  Privacy Policy