Android中基于FCM实现类似Flipkart的优惠推送跳转功能咨询
Hey there! 既然你已经搞定了FCM的基础推送功能,要实现像Flipkart那样点击优惠通知直接跳转到对应活动页的效果,其实核心就是给FCM消息附加跳转参数,再在客户端处理点击后的路由逻辑。下面一步步来拆解实现方案:
实现FCM通知点击跳转指定活动页的完整方案
1. 选对FCM消息类型,灵活控制跳转逻辑
FCM有两种核心消息类型,根据你的场景选最合适的:
- 通知消息:由FCM SDK自动帮你展示通知,适合快速实现基础推送,但点击后的跳转需要额外处理启动页的参数解析。
- 数据消息:完全由你的App客户端接管处理,能自定义通知样式和跳转逻辑,灵活性更高,非常适合这种带定向跳转的优惠活动场景。
2. 后端发送消息时附加跳转标识
不管用哪种消息类型,都要在消息里带上活动页的唯一标识(比如活动ID、页面路由地址)。举个数据消息的后端构造示例:
{ "to": "用户的FCM令牌", "data": { "title": "夏季特惠!全场服饰5折起", "body": "点击直达活动专区", "page_type": "promotion", "activity_id": "summer_sale_2024", "deep_link": "yourapp://promotion/summer_sale_2024" // 可选,用Deep Link的话可以加这个 } }
如果用通知消息,也可以在notification字段外追加data字段,FCM会自动展示通知,同时把数据传递给客户端。
3. 客户端处理通知点击的跳转逻辑
场景一:用FirebaseMessagingService处理数据消息
数据消息不会自动展示通知,你需要在FirebaseMessagingService的onMessageReceived方法里手动创建通知,并设置跳转的PendingIntent:
override fun onMessageReceived(remoteMessage: RemoteMessage) { super.onMessageReceived(remoteMessage) // 解析后端传来的推送数据 val title = remoteMessage.data["title"] ?: "优惠通知" val body = remoteMessage.data["body"] ?: "点击查看详情" val activityId = remoteMessage.data["activity_id"] // 创建跳转到活动页的Intent val intent = Intent(this, PromotionActivity::class.java).apply { putExtra("ACTIVITY_ID", activityId) flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP } val pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) // 构建并显示通知 val notificationBuilder = NotificationCompat.Builder(this, "PROMOTION_CHANNEL_ID") .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(Random().nextInt(), notificationBuilder.build()) }
场景二:处理通知消息的点击跳转
如果发送的是带data字段的通知消息,FCM会自动展示通知,但点击后默认打开App的启动页。这时候要在启动页(比如SplashActivity)里解析传递的数据,再跳转到活动页:
class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // 读取FCM传递的跳转参数 intent.extras?.let { extras -> val activityId = extras.getString("activity_id") if (!activityId.isNullOrEmpty()) { // 直接跳转到对应活动页 val promotionIntent = Intent(this, PromotionActivity::class.java) promotionIntent.putExtra("ACTIVITY_ID", activityId) startActivity(promotionIntent) finish() return } } // 没有跳转参数时,正常进入主页 val mainIntent = Intent(this, MainActivity::class.java) startActivity(mainIntent) finish() } }
4. 进阶:用Deep Link实现更灵活的路由
如果你的App支持Deep Link(比如通过AndroidManifest.xml配置的Scheme或App Links),可以直接在FCM消息里传递Deep Link地址,客户端解析后跳转,这样和Flipkart的实现逻辑更一致:
// 在FirebaseMessagingService里解析Deep Link val deepLink = remoteMessage.data["deep_link"] val intent = Intent(Intent.ACTION_VIEW, Uri.parse(deepLink)) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) // 将pendingIntent绑定到通知上即可
这种方式能统一处理App内外的跳转请求,不管是通知点击还是其他渠道打开Deep Link,都能精准跳转到活动页。
几个关键注意点
- Android 8.0+必须创建通知渠道,否则通知无法正常显示。
- PendingIntent的
FLAG_IMMUTABLE/FLAG_MUTABLE要根据Android版本选择,避免兼容性问题。 - 数据消息不管App在前台还是后台,都会触发
onMessageReceived;而通知消息在App后台时,点击事件会直接传递到启动页,必须在启动页处理参数解析。
内容的提问来源于stack exchange,提问作者Tirumalesh kallepalli
相关产品推荐
相关产品推荐

