在 Localytics 推送通知中实现深度链接
Posted
技术标签:
【中文标题】在 Localytics 推送通知中实现深度链接【英文标题】:Implementing deep linking in Localytics Push Notification 【发布时间】:2015-11-09 06:33:30 【问题描述】:我正在尝试通过 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。
请帮帮我。谢谢!
【问题讨论】:
【参考方案1】:您不需要深层链接来满足您的要求。 Localytics 的人有时会误导开发人员,说您需要对自定义类型的通知进行深度链接。
我们使用 localytics 完成了您希望在应用中执行的相同操作。 1) 在您已经实现的 GCMBroadcastReciever 中接收 Localytics 信息。 2)在您的消息中保留一个字段以识别您要打开的活动
如果您通过以下操作添加了任何额外的类来接收意图
com.google.android.c2dm.intent.RECEIVE
除了你的 GCMReceiver 然后删除它..
这样,所有通知要么来自您的服务器,要么来自本地化,它将在 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()
传递额外的数据
【讨论】:
【参考方案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 是你想要传递给你的活动的任何参数。
【讨论】:
谢谢比尼。上述解决方案适用于我们创建自己的通知构建器的普通推送通知。但是在这里,由于我使用的是 Localytics 推送通知,因此我无法在此处获取“mBuilder”对象来设置 PendingIntent。这就是我卡住的地方..以上是关于在 Localytics 推送通知中实现深度链接的主要内容,如果未能解决你的问题,请参考以下文章