Notice Push on Android with Onesignal - Action in the background

Asked

Viewed 188 times

0

I have in my android application the service Onesignal working properly, so far so good. I want to make some modifications to my Sharepreference (database) each time I receive a message. I managed to do such action while the app is in the foreground, however in the background the action does not work. I would like you to help me solve this problem. Follow the code that works 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");
        }
    }
}

Note that I tried to play in a service, but still do not understand how it works.

1 answer

0


I managed to solve the problem and it is working perfectly, follow the solution found at the following address: Documentation

follows 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 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.