OneSignal-ANDROID-NotificationExtenderService

2023-12-25

从一周前开始我就遇到了问题OneSignal当我的应用程序被杀死时,我点击通知时可以做一些事情。经过研究,我发现我的问题是我没有使用NotificationExtenderService.

但我不知道如何实现这一点。我读到的是,这是作为一个类实现的,但我不明白 - 我是否必须使用该名称创建一个单独的文件类,或者在我的类中创建该类MainActivity?我也不知道去那里做什么。

Thanks!

OneSignal
    .startInit(MagHomeActivity.this)
    .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
    .unsubscribeWhenNotificationsAreDisabled(true)
    .setNotificationReceivedHandler(new OneSignal.NotificationReceivedHandler() {
        @Override
        public void notificationReceived(OSNotification notification) {
            final String notificationID = notification.payload.notificationID;
            JSONObject tags = new JSONObject();
            try {
                MagServices service = MagApplication.getRetrofitAuth(MagHomeActivity.this)
                                                    .create(MagServices.class);
                Call<ResponseBody> magazines = service.registerNotification(notificationID);
                magazines.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    })
    .setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            Intent intent = new Intent(MagHomeActivity.this,MagMyMessageActivity.class);
            startActivity(intent);
        }
    })
    .init();

您可以将其放入专门针对该类的单独文件中。例如...

通知扩展器示例.java

import android.support.v4.app.NotificationCompat;
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import java.math.BigInteger;

public class NotificationExtenderExample extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      OverrideSettings overrideSettings = new OverrideSettings();
      overrideSettings.extender = new NotificationCompat.Extender() {
         @Override
         public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
            // Sets the background notification color to Green on Android 5.0+ devices.
            return builder.setColor(new BigInteger("FF00FF00", 16).intValue());
         }
      };

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
      Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);

      return true;
   }
}

然后,将以下内容添加到您的AndroidManifest.xml

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

SOURCE: https://documentation.onesignal.com/docs/service-extensions#section-notification-extender-service-span-class-label-all-label-android-android-span- https://documentation.onesignal.com/docs/service-extensions#section-notification-extender-service-span-class-label-all-label-android-android-span-

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OneSignal-ANDROID-NotificationExtenderService 的相关文章

随机推荐