2

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

     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。

请帮我解决这个问题。谢谢!

4

2 回答 2

3

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

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

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

com.google.android.c2dm.intent.RECEIVE

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

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

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

安卓清单.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还是从你自己的服务器发送包含字段的GCM消息,"message_type"="news_notification"然后用户点击通知将打开SingleNEwsActivity,如果"message_type"=event_notification"然后它将打开SingleEventActivity..你也可以在这里传递额外的数据notificationIntent.putExtra()

于 2015-11-09T08:47:53.873 回答
2

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

// 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 是您想要传递给活动的任何参数。

于 2015-11-09T07:02:17.373 回答