简体   繁体   English

如何在Android中测试FCM推送通知

[英]How to test FCM push notifications in Android

I'm writing a FCM notification integration & it's working fine. 我正在编写FCM通知集成,并且工作正常。 To test component is there a way to mock RemoteMessage object? 要测试组件,是否有一种模拟RemoteMessage对象的方法?

By mocking I mean to perform Junit test case. 通过模拟,我的意思是执行Junit测试用例。 Any suggestion ! 任何建议!

There is currently no way to mock a RemoteMessage object used in FCM. 当前RemoteMessage无法模拟FCM中使用的RemoteMessage对象。 AFAIK, there isn't even a way to test the onMessageReceived() method without using the actual service. AFAIK,甚至不使用实际服务就无法测试onMessageReceived()方法。

What is only valid to do is to test your sendNotification() (or whatever you named your method) that hopefully accepts a Hashmap that may come from the RemoteMessage object (this is of course if you're only using a data message payload . 唯一有效的方法是测试您的sendNotification() (或任何您命名的方法),该方法有望接受可能来自RemoteMessage对象的RemoteMessage (当然,如果您仅使用数据消息有效负载,则可以这样做)。

Unfortunately, you can't, and there are no signs of supporting it in the near future. 不幸的是,您不能这样做,并且在不久的将来没有支持它的迹象。

What you can do: what we did, was to build an API command in your server, that would send you a test push. 您可以做什么我们所做的就是在服务器中构建一个API命令,该命令将向您发送测试推送。 It would be tedious to support, but that's as good as it gets. 支持会很乏味,但是就足够了。

Even worse: suppose that the testing is done to guarantee reliability. 更糟糕的是:假设已进行测试以确保可靠性。 What if it fails? 如果失败了怎么办? You request a new token but FCM will give you the same one, probably... but it does not work! 您请求一个新令牌,但FCM可能会给您相同的令牌,但是它不起作用! If you invalidate and request a token - will it help? 如果您使令牌无效并请求令牌-会有所帮助吗? Not sure. 不确定。

Your options are limited indeed. 您的选择确实有限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM