Push notification on Android with OneSignal - Background action

0

I have in my android application the OneSignal service working correctly, so far so good. I want to make some modifications to my SharePreference every time I receive a message. I was able to do this while the application is in the foreground, however in the background the action does not work. I would then like to help me solve this problem. Here is the code that works at first prano

public class mIntentService extends IntentService {
    public mIntentService() {
        super("notification");

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        // OneSignal Initialization
        OneSignal.startInit(MyApplication.getAppContext())
                .setNotificationReceivedHandler(new NotificationReceivedHandler())
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();


        Log.i("notification", "O IntentService foi iniciado");
    }

    public class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {

            //Essa é a função após receber a notificação
            LibraryIO io = new LibraryIO(getApplicationContext());
            io.setBooleanIO("iconNotificationNews", true);
            Log.i("iconNotificationNews", "iconNotificationNews chamadado");
        }
    }
}

Please note that I tried to play on a service, but I still do not understand how it works.

    
asked by anonymous 15.06.2018 / 16:35

1 answer

0

I was able to solve the problem and it works perfectly, it follows the solution found in the following address: Documentation

follow my code:

import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.edesonabizerril.newintercampi.data_and_utils.LibraryIO;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.OSNotificationReceivedResult;
import java.math.BigInteger;

public class mIntentService extends NotificationExtenderService{

    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
    LibraryIO io = new LibraryIO(getApplicationContext());
    io.setBooleanIO("iconNotificationNews", true);
    Log.i("iconNotificationNews", "iconNotificationNews chamadado");

    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.
            Log.i("iconNotificationNews", "iconNotificationNews chamadado 2");
            return builder.setColor(new BigInteger("0d2541", 16).intValue());
        }
    };

    OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
    Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
    Log.i("iconNotificationNews", "iconNotificationNews chamadado 3");
    return true;
    }
}

On the main screen we have:

// OneSignal Initialization
        OneSignal.startInit(MyApplication.getAppContext())
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

And below all the activities there is the following code in the manifest file

<service
            android:name=".services.mIntentService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false">
            <intent-filter>
                <action android:name="com.onesignal.NotificationExtender" />
            </intent-filter>
        </service>
    
15.06.2018 / 23:12