简体   繁体   中英

Implementing deep linking in Localytics Push Notification

I am trying to implement Deep linking through Localytics Push notification in Android.In the below code I am able to receive the key value pair which I send through Localytics dashboard while creating the Push notification. However, my requirement is to open a specific activity based on the key/value pair I receive in Push Notification.

     public class GCMReceiver extends BroadcastReceiver {
     String deeplink_key = "KEY_DEEPLINK";
     public static final String CUSTOM_INTENT ="com.mypackage.action.TEST";

     @Override
     public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    String deeplinkValues = extras.getString(deeplink_key);
    Log.i("BASE", "deeplinkValues: " + deeplinkValues);
    String action = intent.getAction();
    Uri data = intent.getData();

    Intent gotoOffersIntent = new Intent(context,OffersDisplayActivity.class);
    gotoOffersIntent.putExtra(deeplink_key, deeplinkValues);
//  gotoOffersIntent.setAction(CUSTOM_INTENT);
    /*The below line opens the OffersDisplayActvity directly when Push notification is received*/
    context.startActivity(gotoOffersIntent);


//  context.sendOrderedBroadcast(gotoOffersIntent, null);

    PushReceiver pushReceiver = new PushReceiver();
    pushReceiver.onReceive(context, intent);

    GCMBroadcastReceiver gcmBroadcastReceiver = new GCMBroadcastReceiver();
    gcmBroadcastReceiver.onReceive(context, intent);

}
}

With the above code I am able to open OffersDisplayActivity on PushNotification received,But I want the OffersDisplayActivity to be opened when I click on the Push notification.

Please help me with this.Thank you!

You don't need deep linking for your requirement. Localytics guys sometimes misinforms developers by saying you need deeplinking for custom type of notifications.

We did same thing which you want to do in your app with localytics. 1)Receive Localytics information in your already implemented GCMBroadcastReciever. 2)In you message keep one field for identifying which Activity you want to open

If you added any extra Class for receiving intent with following action

com.google.android.c2dm.intent.RECEIVE

apart from your GCMReceiver then remove it..

In that way all notification either came from your server or localytics it will be received in onReceive method.

Here is complete example what we did for localytics and our own server..

Android Manifest.xml

<service
            android:name=".gcm.CustomInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- for Gingerbread GSF backward compat -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.nearfox.android" />
            </intent-filter>
        </receiver>

        <service android:name=".gcm.CustomGCMListenerService">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".gcm.RegistrationIntentService"
            android:exported="false" />

in CustomGCMListenerService.java

public class CustomGCMListenerService extends GcmListenerService {

    private static final String TAG = "CustomGCMListener";

    public interface MESSAGE_TYPE {
        String NOTIFICATION_NEWS = "news_notification";
        String NOTIFICATION_EVENT = "event_notification";
    }

    @Override
    public void onMessageReceived(String from, Bundle data) {
        if (data.containsKey("msg_type") && data.getString("msg_type") != null) {
            String messageType = data.getString("msg_type");
            if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_NEWS)) {
                String newsJson = data.getString("news_body");
                try {
                    JSONObject jsonObject = new JSONObject(newsJson).getJSONObject("message");
                    generateNotification(this, jsonObject.getString("title"), "", MESSAGE_TYPE.NOTIFICATION_NEWS, data);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i(TAG, "Notification Parsing Error");
                    return;
                }
            } else if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_EVENT)) {
                String newsJson = data.getString("body");
                try {
                    JSONObject jsonObject = new JSONObject(newsJson).getJSONObject("message");
                    generateNotification(this, jsonObject.getString("title"), "", MESSAGE_TYPE.NOTIFICATION_EVENT, data);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i(TAG, "Notification Parsing Error");
                    return;
                }
            }
        }
    }


    public static void generateNotification(Context context, String message, String ids, String messageType, Bundle data) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
        notificationBuilder.setSmallIcon(R.drawable.small_notification_icon);
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon));
        String title = context.getString(R.string.app_name);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        Notification notification ;


        if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_NEWS)) {
            Intent notificationIntent = new Intent(context, SingleNewsActivity.class);
            notificationIntent.putExtra("source", "notification");
            notificationIntent.putExtra("news_title", message);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(intent);
        } else if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_EVENT)) {
            Intent notificationIntent = new Intent(context, SingleEventActivity.class);
            notificationIntent.putExtra("source", "notification");
            notificationIntent.putExtra("event_title", data);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(intent);
        }
        notificationBuilder.setContentText(message);
        notificationBuilder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(message));
        notification = notificationBuilder.build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

    }
}

So here you can see if from localytics or from your own server you send GCM message which contains field "message_type"="news_notification" then user click on notification will open SingleNEwsActivity and if "message_type"=event_notification" then it will open SingleEventActivity.. also here you can pass extra data with notificationIntent.putExtra()

Compare your key-value pair and based on it, call desire activity from Intent while generating push notification. It will call it when user taps on notification.

// Set the action to take when a user taps the notification
    Intent resultIntent = new Intent(context, LoginActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if (notificationObj!=null) {
        resultIntent.putExtra(UserDefault.pushJSONObj, notificationObj);
    }

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

Here notificationObj is if any parameter you want to pass to your activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM