You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Android应用接收GCM通知添加测试覆盖?

解决GCM通知后台到达的自动化测试问题

首先,结合你提到的测试场景和遇到的API 26通知不显示的痛点,给你一套落地的自动化测试方案,覆盖通知触发、验证、点击跳转全流程:

一、核心测试需求梳理

  • 应用处于后台时,模拟GCM通知到达
  • 验证通知在不同API版本(重点API 26+)下正常显示
  • 点击通知后验证应用成功打开

二、用Intent模拟GCM通知触发(适配API 26+)

你提到已经找到可行的Intent创建方式,这里补充适配API 26通知渠道要求的完整实现:
Android API 26开始强制要求通知渠道,未创建对应渠道的通知会被系统拦截,这也是你之前遇到通知不显示的核心原因之一。

1. 初始化通知渠道

private void createGcmNotificationChannel(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "GCM推送通知渠道";
        String channelDesc = "用于接收GCM推送的通知渠道";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        
        NotificationChannel channel = new NotificationChannel("GCM_TEST_CHANNEL", channelName, importance);
        channel.setDescription(channelDesc);
        
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

2. 构建并发送模拟GCM通知

// 先初始化通知渠道
createGcmNotificationChannel(instrumentation.getContext());

// 构建点击通知后打开应用的PendingIntent
Intent appLaunchIntent = instrumentation.getContext().getPackageManager()
        .getLaunchIntentForPackage("com.your.app.package");
appLaunchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
        instrumentation.getContext(), 
        0, 
        appLaunchIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// 构建通知内容
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
        instrumentation.getContext(), 
        "GCM_TEST_CHANNEL"
)
        .setSmallIcon(R.drawable.ic_notification_icon)
        .setContentTitle("GCM测试通知")
        .setContentText("这是一条模拟的GCM推送通知")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);

// 发送通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(instrumentation.getContext());
notificationManager.notify(1, notificationBuilder.build());

三、用UiAutomator完成通知验证与点击操作

1. 验证通知是否显示

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// 下拉打开通知栏
device.openNotification();
// 等待通知加载完成
device.wait(Until.hasObject(By.text("GCM测试通知")), 5000);
// 检查通知是否存在
UiObject targetNotification = device.findObject(new UiSelector().text("GCM测试通知"));
assertTrue("GCM通知未正常显示", targetNotification.exists());

2. 点击通知并验证应用打开

// 点击目标通知
targetNotification.click();
// 等待应用启动,验证当前包名是否匹配目标应用
device.wait(Until.hasObject(By.pkg("com.your.app.package").depth(0)), 10000);
String currentPackage = device.getCurrentPackageName();
assertEquals("点击通知后未成功打开应用", "com.your.app.package", currentPackage);

四、针对API 26的额外检查点

可以在测试中加入渠道有效性验证,提前排查适配问题:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager notificationManager = instrumentation.getContext().getSystemService(NotificationManager.class);
    NotificationChannel channel = notificationManager.getNotificationChannel("GCM_TEST_CHANNEL");
    assertNotNull("GCM通知渠道未创建", channel);
    assertEquals("通知渠道优先级设置错误", NotificationManager.IMPORTANCE_DEFAULT, channel.getImportance());
}

补充说明:你之前查阅的内容聚焦于检测已存在的通知,而我们通过直接构建Intent发送通知,完全模拟GCM推送的触发逻辑,更贴近真实业务场景,也能覆盖API 26的渠道适配问题。

内容的提问来源于stack exchange,提问作者TimCO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:47:01