package me.pushy.sdk;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.media.RingtoneManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;

public class PushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Notification title and text
        String notificationTitle = getAppName(context);
        String notificationText = "";

        // Attempt to extract the notification text from the "message" property of the data payload
        if (intent.getStringExtra("message") != null) {
            notificationText = intent.getStringExtra("message");
        }

        // Prepare a notification with vibration and sound
        Notification.Builder builder = new Notification.Builder(context)
                .setAutoCancel(true)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setVibrate(new long[]{0, 400, 250, 400})
                .setSmallIcon(context.getApplicationInfo().icon)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(getMainActivityPendingIntent(context));

        // Get an instance of the NotificationManager service
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        // Build the notification and display it
        notificationManager.notify(1, builder.build());
    }

    private static String getAppName(Context context) {
        // Attempt to determine app name via package manager
        return context.getPackageManager().getApplicationLabel(context.getApplicationInfo()).toString();
    }

    private PendingIntent getMainActivityPendingIntent(Context context) {
        // Get launcher activity intent
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationContext().getPackageName());

        // Make sure to update the activity if it exists
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Convert intent into pending intent
        return PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}
@@ -16,16 +16,41 @@
 y.sdk;%0A%0A
+import android.os.Build;%0A
 import a
@@ -74,16 +74,16 @@
 cation;%0A
-
 import a
@@ -290,16 +290,113 @@
 eceiver;
+%0Aimport android.util.Log;%0A%0Aimport java.lang.reflect.Constructor;%0Aimport java.lang.reflect.Method;
 %0A%0Apublic
@@ -1631,24 +1631,191 @@
 _SERVICE);%0A%0A
+        // Device is Android O or newer?%0A        if (Build.VERSION.SDK_INT %3E= 26) %7B%0A            configureNotificationChannel(builder, notificationManager);%0A        %7D%0A%0A
         // B
@@ -2586,32 +2586,32 @@
  pending intent%0A
-
         return P
@@ -2702,10 +2702,1923 @@
 );%0A    %7D
+%0A%0A    private void configureNotificationChannel(Notification.Builder builder, NotificationManager notificationManager) %7B%0A        // Channel details%0A        String channelId = %22pushy%22;%0A        String channelName = %22Push Notifications%22;%0A%0A        // Channel importance (4 means high importance)%0A        int channelImportance = 4;%0A%0A        try %7B%0A            // Get NotificationChannel class via reflection (only available on API level 26+)%0A            Class notificationChannelClass = Class.forName(%22android.app.NotificationChannel%22);%0A%0A            // Get NotificationChannel constructor%0A            Constructor%3C?%3E notificationChannelConstructor = notificationChannelClass.getDeclaredConstructor(String.class, CharSequence.class, int.class);%0A%0A            // Instantiate new notification channel%0A            Object notificationChannel = notificationChannelConstructor.newInstance(channelId, channelName, channelImportance);%0A%0A            // Get notification channel creation method via reflection%0A            Method createNotificationChannelMethod = notificationManager.getClass().getDeclaredMethod(%22createNotificationChannel%22, notificationChannelClass);%0A%0A            // Invoke method on NotificationManager, passing in the channel object%0A            createNotificationChannelMethod.invoke(notificationManager, notificationChannel);%0A%0A            // Get %22setChannelId%22 method for Notification.Builder (only for AppCompat v26+)%0A            Method setChannelIdMethod = builder.getClass().getDeclaredMethod(%22setChannelId%22, String.class);%0A%0A            // Invoke method, passing in the channel ID%0A            setChannelIdMethod.invoke(builder, channelId);%0A%0A            // Log success to console%0A            Log.d(%22Pushy%22, %22Notification channel set successfully%22);%0A        %7D catch (Exception exc) %7B%0A            // Log exception to console%0A            Log.e(%22Pushy%22, %22Creating notification channel failed%22, exc);%0A        %7D%0A    %7D
 %0A%7D

Support for Android O Notification Channels