背景:
我希望在我的安卓应用程序中轻松模拟接收推送通知的方法。
我使用SNS和GCM发送推送通知,但我不想每次测试接收器处理时都发送真正的通知。
我在主活动中创建了一个“开发菜单”,其中包含“清除缓存”、“伪造服务器错误”等项目,因此我添加了一个“伪造推送通知”项目到该列表中。(仅出现在调试版本中)
这个想法是,当选择菜单项时,我会以编程方式调用onMessageReceived方法来实现GcmListenerService,从而伪造接收到推送通知。
问题
为了访问onMessageReceived方法,我需要访问GcmListenerService,该服务在我的清单文件中定义如下(根据文档):<service
android:name=".service.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我尝试使用现有的Dagger2设置将一个实例注入到活动中,但我不知道如何向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提供者:(我不希望它能够工作,因为它不是正确的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
}
}
我认为我可能一直在走错路,因此对于任何其他技术都持开放态度。但请不要建议涉及应用程序外部的选项(如某些虚假服务器之类),因为整个重点是使用仅在应用程序中可用的操作来欺骗应用程序内的通知... 这也很有用,可以向利益相关者演示通知。