在 Localytics 推送通知中实施深度链接

Implementing deep linking in Localytics Push Notification

我正在尝试通过以下代码中的 Localytics 推送通知实现深度链接 Android.In 我能够接收在创建推送通知时通过 Localytics 仪表板发送的键值对。 但是,我的要求是根据我在推送通知中收到的 key/value 对打开特定的 activity。

     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);

}
}

使用上面的代码,我可以在收到 PushNotification 时打开 OffersDisplayActivity,但是我希望在单击推送通知时打开 OffersDisplayActivity。

请帮我this.Thank你!

比较您的键值对并基于它,在生成推送通知时从 Intent 调用 desire activity。 它会在用户点击通知时调用它。

// 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);

此处 notificationObj 是您要传递给 activity 的任何参数。

您的要求不需要深层链接。 Localytics 的家伙有时会误导开发人员,说您需要为自定义类型的通知进行深层链接。

我们用 localytics 做了你想在你的应用程序中做的同样的事情。 1) 在您已经实施的 GCMBroadcastReciever 中接收 Localytics 信息。 2) 在您的消息中保留一个字段用于识别您要打开的Activity

如果您添加了任何额外的 Class 以通过以下操作接收意图

com.google.android.c2dm.intent.RECEIVE

除了你的 GCMReceiver 然后删除它..

这样,所有通知要么来自您的服务器,要么来自 localytics,它们将在 onReceive 方法中接收。

这是我们为 localytics 和我们自己的服务器所做的完整示例..

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" />

在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);

    }
}

所以在这里你可以看到你是从 localytics 还是从你自己的服务器发送包含字段 "message_type"="news_notification" 的 GCM 消息然后用户点击通知将打开 SingleNEwsActivity 如果 "message_type"=event_notification" 那么它将打开 SingleEventActivity.. 在这里你还可以使用 notificationIntent.putExtra()

传递额外的数据