calcazar / react-native-push-notification-CE

The community edition of the push notification library (by Zo0r) for applications made with React Native
MIT License
52 stars 23 forks source link

[Android] How to create heads-up notification? #9

Open xvonabur opened 6 years ago

xvonabur commented 6 years ago

Hello! Is there a way to create heads-up notification with this library? I've tried to create local notification manually with default sound and number as 10, but it didn't pop up. Am I missing something?

xvonabur commented 6 years ago

@avencat Android, as issue titled.

cristianonescu commented 5 years ago

@xvonabur did you find a solution for this?

xvonabur commented 5 years ago

@cristianonescu I ended up using wix/react-native-notifications . It also has some flaws, but you can check my fork for more info: xvonabur/react-native-notifications.

danniarreza commented 5 years ago

@xvonabur hey, I'm trying to implement notification feature to the react native app that I'm working on right now. As I'm following the instruction from wix/react-native-notifications, I couldn't find any instruction nor example about heads up notification implementation for android especially. Could you elaborate more for this? Thank you

xvonabur commented 5 years ago

@danniarreza you can do it on native side of you app. Create CustomPushNotification.java class and implement getNotificationBuilder there. You can call

final Notification.Builder builder = new Notification.Builder(cContext).setPriority(android.app.Notification.PRIORITY_HIGH)

inside it to show push message as heads-up. For Android 8 you need to pass NotificationManager.IMPORTANCE_HIGH to NotificationChannel.

Full example of CustomPushNotification file:

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.app.NotificationChannel;
import android.app.NotificationManager;

import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.BitmapLoader;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.core.notifications.LocalNotification;
import com.wix.reactnativenotifications.core.notifications.NotificationProps;

import static android.app.Notification.PRIORITY_HIGH;

public class CustomPushNotification extends LocalNotification {
    private NotificationProps cNotificationProps;
    private Context cContext;

    public CustomPushNotification(Context context, NotificationProps notificationProps,
                                  AppLifecycleFacade facade, AppLaunchHelper defaultAppLaunchHelper,
                                  JsIOHelper jsIOHelper, BitmapLoader bitmapLoader) {
        super(context, notificationProps, facade, defaultAppLaunchHelper, jsIOHelper, bitmapLoader);
        cNotificationProps = notificationProps;
        cContext = context;
    }

    @Override
    protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
        String CHANNEL_ID = "general_channel";// The internal id of the channel.
        String CHANNEL_NAME = "Notifications";// The public name of the channel.

        final Integer icon = cNotificationProps.getIcon();

        final Notification.Builder builder = new Notification.Builder(cContext)
                .setContentTitle(cNotificationProps.getTitle())
                .setContentText(cNotificationProps.getBody())
                .setSmallIcon(icon != null ? icon : R.drawable.ic_notification)
                .setSound(cNotificationProps.getSound())
                .setContentIntent(intent)
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(PRIORITY_HIGH)
                .setStyle(new Notification.BigTextStyle().bigText(cNotificationProps.getBody()))
                .setAutoCancel(true);

        final Integer color = cNotificationProps.getColor();

        if (color != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(color);
        }

        final Integer lightsColor = cNotificationProps.getLightsColor();
        final Integer lightsOnMs = cNotificationProps.getLightsOnMs();
        final Integer lightsOffMs = cNotificationProps.getLightsOffMs();

        if (lightsColor != null && lightsOnMs != null && lightsOffMs != null) {
            builder.setLights(lightsColor, lightsOnMs, lightsOffMs);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                                                                CHANNEL_NAME,
                                                                NotificationManager.IMPORTANCE_HIGH);

          builder.setChannelId(CHANNEL_ID);

          final NotificationManager notificationManager =
            (NotificationManager) cContext.getSystemService(Context.NOTIFICATION_SERVICE);
          notificationManager.createNotificationChannel(channel);
        }

        return builder;
    }
}