Search code examples
javaandroidpush-notificationonesignal

How to handle click events on One Signal action buttons when the app is not running (Android Java)


I am trying to add click listener on the one signal action button, but when the app is not running, how can I listen the click event on that button and against that I just want to log a simple message on console. How can I do that? I am using One Signal 5.0.2 version. Here's my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.notificationdemo">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- Add this for Android 11+ -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <permission
        android:name="com.onesignal.sdktest.permission.RECEIVE_ADM_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.onesignal.sdktest.permission.RECEIVE_ADM_MESSAGE" />
    <!-- This permission allows your app access to receive push notifications from ADM. -->
    <uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@drawable/notification_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.NotificationDemo"
        tools:targetApi="31">
        <meta-data android:name="com.onesignal.NotificationServiceExtension"
            android:value="com.example.notificationdemo.MainActivity" />

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is my code snippet:

public class MainActivity extends AppCompatActivity implements INotificationServiceExtension {

    Button login, logout;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OneSignal.initWithContext(this, "beaced0c-d1e4-4995-b811-392a02c05cd5");
        OneSignal.getNotifications().requestPermission(true, Continue.none());

        OneSignal.getDebug().setLogLevel(LogLevel.DEBUG);
        String playerId =         @Override
    public void onNotificationReceived(@NonNull INotificationReceivedEvent event) {
        Log.v("logtag:", "IRemoteNotificationReceivedHandler fired" + " with INotificationReceivedEvent: " + event.toString());

        IDisplayableMutableNotification notification = event.getNotification();

        if (notification.getActionButtons() != null) {
            for (IActionButton button : notification.getActionButtons()) {
                Log.v("actionbuttontag: ", "ActionButton: " + button.toString());

            }
        }

    }
}

Solution

  • You have 𝑛 + 1 iterations: one for 𝑖=𝑛, then 𝑛−1, 𝑛−2, ..., 2, 1, 0.

    Please let me know the answer in the summation notation.

    The mathematical notation for sums, usually sets the lower value at the bottom and the maximum value at the top. The order for summation is irrelevant here. Assuming that multiplication is regarded as a O(1) operation, you have this sum:

    𝑖𝑛= 0 O(1) = O(𝑛+1) = O(𝑛)

    Just to be clear, nor the result of the code (the value of X) nor the time complexity depend on whether i goes from n to 0 or vice versa. So this code achieves the same with the same time complexity:

    for i=0 to n do
        X=X*2
    end for