本文介绍了OneSignal-ANDROID-NotificationExtenderService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从一周前开始,我遇到了 OneSignal 的问题,并且在我的应用被终止时点击通知时正在做一些事情.经过研究,我发现我的问题是我没有使用 NotificationExtenderService .

I'm having problems since a week ago with OneSignal and doing stuff when I tap on the notification when my app is killed. After research, I've found out that my problem is that I'm not using the NotificationExtenderService.

但是我不知道该如何实现.我读到的是,这是作为一个类实现的,我不明白-我是否必须使用该名称创建一个单独的文件类,还是在我的 MainActivity 内部创建该类?我也不知道该怎么办.

But I don't know how to implement this. What I read is that this is implemented as a Class and I don't get it - do I have to create a separated file class with that name, or create the class inside of my MainActivity? I don't know what to do there too.

谢谢!

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();

推荐答案

您可以将其放在专门用于该类的单独文件中.例如...

You can just put it in a separate file specifically for that class. For example...

NotificationExtenderExample.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

Then, add the following to your 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>

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

这篇关于OneSignal-ANDROID-NotificationExtenderService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:23