gpt4 book ai didi

android - 在 Localytics 推送通知中实现深度链接

转载 作者:行者123 更新时间:2023-11-29 15:09:17 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

com.google.android.c2dm.intent.RECEIVE

除了您的 GCMReceiver 然后将其删除..

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

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

安卓 list .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()

传递额外数据

关于android - 在 Localytics 推送通知中实现深度链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com