在 Android 中点击 One Signal 推送通知时如何导航到特定页面?

Posted

技术标签:

【中文标题】在 Android 中点击 One Signal 推送通知时如何导航到特定页面?【英文标题】:How to navigate to specific page when tapped on One Signal push notification in Android? 【发布时间】:2021-09-14 07:57:42 【问题描述】:

我有一个电子商务 android 应用程序,我在其中使用 One Signal 进行推送通知。我的应用中有两种类型的通知。

    每当商品有新优惠时,管理员都会推送一条通知,如果用户点击此通知,它应该直接打开商品详情屏幕。 订单状态。如果订单状态发生变化,用户将收到通知,如果用户点击此通知,则应直接打开订单详细信息屏幕。

如何使用 One Signal 在 android 中实现这些功能?

【问题讨论】:

【参考方案1】:

在 OneSignal 服务类中

public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler 

@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) 
    OSNotification notification = notificationReceivedEvent.getNotification();

    // Example of modifying the notification's accent color
    OSMutableNotification mutableNotification = notification.mutableCopy();
    mutableNotification.setExtender(builder -> 
        //... do stuff
        builder.setTimeoutAfter(30000);
        Intent intent = new Intent();
                JSONObject data = notification.payload.additionalData;
              
               // check the data and create intent
                intent = new Intent(getApplicationContext(), OrderDetailScreen.class); 
               // or any other depends on data value
               intent.putExtra("data", data);
               PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                newbuilder[0] = builder.setContentIntent(pendIntent);
        return builder;
    );
    JSONObject data = notification.getAdditionalData();
    Log.i("OneSignalExample", "Received Notification Data: " + data);

    // If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
    // To omit displaying a notification, pass `null` to complete()
    notificationReceivedEvent.complete(mutableNotification);

【讨论】:

以上是关于在 Android 中点击 One Signal 推送通知时如何导航到特定页面?的主要内容,如果未能解决你的问题,请参考以下文章

如何在iOS中获得One Signal用户的唯一玩家ID?

如何在 Mobile 中获取 One Signal 唯一用户 ID?

One Signal REST API - 获取模板列表

android中TextView的背景图切换问题

如何将Android studio中Library发布到Jcenter

android studio搜不到插件