OneSignal - ANDROID - NotificationExtenderService

≡放荡痞女 提交于 2019-12-11 12:32:32

问题


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.

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.

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

回答1:


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;
   }
}

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>

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



来源:https://stackoverflow.com/questions/56580587/onesignal-android-notificationextenderservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!