android - 在 Android 应用程序中伪造 GCM 消息

标签 android google-cloud-messaging gcmlistenerservice

背景:

我想要一种简单的方法来假装在我的 Android 应用程序中收到推送通知。

我使用 SNS 和 GCM 发送推送通知,但我不想每次测试接收器处理时都必须发送真实的通知。

我在主要 Activity 中创建了一个“开发菜单”,其中包含“清除缓存”、“虚假服务器错误”等项目,因此我向该列表添加了一个“虚假推送通知”项目。 (这只出现在调试版本中)

我的想法是以编程方式调用 onMessageReceived我执行 GcmListenerService 的方法选择菜单项时,要伪造收到的推送通知。

问题

为了访问 onMessageReceived 方法,我必须访问 GcmListenerService,这是在我的 list 中定义的服务(根据文档):

<service
    android:name=".service.MyGcmListenerService"
    android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
</service>

我已经尝试使用我现有的 Dagger2 设置将一个实例注入(inject) Activity ,但我不知道如何“提供”正确的实例给 Dagger 。

新建一个 MyGcmListenerService 显然调用了 onMessageReceived 方法,但没有应用程序上下文,所以我在创建 Intent 时得到了一个 NPE:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:128)at android.content.Intent.<init>(Intent.java:4868)
at uk.co.blah.blah.service.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:30)
at uk.co.blah.blah.DashboardActivity.onOptionsItemSelected(DashboardActivity.java:297)
at android.app.Activity.onMenuItemSelected(Activity.java:3203)

Dagger Provider:(我不希望它起作用,因为它不是正确的 GcmListenerService)

@Provides
@Singleton
MyGcmListenerService provideMyGcmListenerService() {
    return new MyGcmListenerService();
}

菜单项处理程序:

if (id == R.id.fake_notification) {
        Bundle bundle = new Bundle();
        bundle.putString("message", "Test notification");
        gcmListenerService.onMessageReceived("fakeNotification", bundle);
}

我对 GcmListenerService 的实现:

public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Timber.d("From: %s", from);
    Timber.d("Message: %s", message);

    // FIXME Fails here
    Intent notificationAction = new Intent(this, DashboardActivity.class); 

    ... do some other stuff to create the notification

}

我想我可能会把这一切都弄错了,所以我愿意接受任何其他技术。但是,请不要建议涉及应用程序外部(一些假服务器或其他东西)的选项,因为这样做的全部目的是在应用程序内欺骗通知,仅使用应用程序中可用的操作...对演示也很有用通知利益相关者等

最佳答案

可能最简单的方法是让您的 MyGcmListenerService.onMessageReceived 调用一些方法,然后您的测试调用相同的方法。

但是如果你真的想实例化你的MyGcmListenerService,那么你需要遵循usual steps to start and connect to an Android Service .

关于android - 在 Android 应用程序中伪造 GCM 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38374340/

相关文章:

java - 使用 libGDX 写入 Json

android - 仅接收来自 GCM 的最后一个后台静默推送通知

firebase - GCM3 和 FCM 向后兼容 GCM 2

google-cloud-messaging - 同一应用程序中的多个 GCM 发送器

android - GcmListenerService.onMessageReceived() 仅在应用程序运行时调用

java - 如何通过 Volley 将旋转器数据发送到数据库

java - 文本转语音保存到音频文件,静音(长时间停顿)

java - 无法更改嵌套 fragment 中的 ActionBar 标题

安卓 : GCM Push Notification to particular device

android - 关闭应用程序时不会调用 GCMListenerService