如何在安卓手机上屏蔽Onesignal的推送通知?

4
我知道我可以在BroadcastOnReceive()方法中接收它们,但我无法阻止OneSignal创建一个Notification

1
非常感谢所有没有留下评论的投票者。真是个好主意。。 - Majstor
2个回答

7
您需要设置一个 OneSignal 的 NotificationExtenderService 类并将其添加到您的 AndroidManifest.xml 文件中作为一个 Service。
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;

public class NotificationExtenderBareBonesExample extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
        // Read properties from result.

      // Returning true tells the OneSignal SDK you have processed the notification and not to display it's own.
      return true;
   }
}

AndroidManifest.xml

<service
   android:name=".NotificationExtenderBareBonesExample"
   android:permission="android.permission.BIND_JOB_SERVICE"
   android:exported="false">
   <intent-filter>
      <action android:name="com.onesignal.NotificationExtender" />
   </intent-filter>
</service>

请查看OneSignal Android文档中的“Background Data and Notification Overriding”部分,以获取更多详细信息。

谢谢@jkasten,这对我帮助很大。给了我一个想法,我可以在Onesignal仪表板中以键值对的形式发送所需的参数。 - Majstor
@Majstor,您能否提供一下如何隐藏OneSignal通知的代码? - karanatwal.github.io
@KarandeepAtwal 我更新了我的答案,以符合3.0 SDK中的可用内容。 - jkasten
同时,您可以将以下行添加到AndroidManifest中的服务标签中:android:permission="android.permission.BIND_JOB_SERVICE" - Ali Zarei
@AliZarei 感谢提供 Android Oreo (8.0) 必需的更新。我已将其添加到我的答案中。 - jkasten
显示剩余2条评论

0
NotificationExtenderService找不到了
改用这个:
OSRemoteNotificationReceivedHandler 并添加到AndroidManifest.xml中
    <meta-data android:name="com.onesignal.NotificationServiceExtension"
        android:value=".notifications.RemoteNotificationReceivedHandler" />

另外,请访问以下链接:服务扩展

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接