Search code examples
javaandroidandroid-notificationsandroid-notification-barandroid-timer

Android Notification is not Showing Java for Stop Watch


It's me again, and now I'm currently building an app for a stopwatch, alarm, etc. So I've added service and notification into my code but the thing is that the only one which runs perfectly is the main view or simply I called it "TimePlayer.java", this one is my stopwatch activity. The notification didn't show up, I've checked my code several times, but have no ideas which part that I missed. I'm using target API OREO

this one is my Service Class :

public class MyServices extends Service implements PropertyChangeListener {
    //..........................................................

    private static final int NOTIFICATION_ID = 0;

    private NotificationManager mNotificationManager;
    private boolean isNotificationShowing;
    private BroadcastReceiver receiverStateChanged;
    private BroadcastReceiver receiverResets;
    private BroadcastReceiver receiverExit;
    private Timer t;
    private Builder mBuilder;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        isNotificationShowing = false;
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        IntentFilter filterNext = new IntentFilter(ACTION_PLAY);
        receiverStateChanged = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
                    TimeContainer.getInstance().pause();
                } else {
                    TimeContainer.getInstance().start();
                }
                updateNotification();
            }
        };
        registerReceiver(receiverStateChanged, filterNext);
        receiverResets = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TimeContainer.getInstance().reset();
                updateNotification();
            }
        };
        IntentFilter filterPause = new IntentFilter(ACTION_RESET);
        registerReceiver(receiverResets, filterPause);
        IntentFilter filterPrev = new IntentFilter(ACTION_STOP);
        receiverExit = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TimeContainer.getInstance().reset();
                stopForeground(true);
                isNotificationShowing = false;
                stopSelf();
            }
        };
        registerReceiver(receiverExit, filterPrev);
        startUpdateTimer();
        TimeContainer.getInstance().isServiceRunning.set(true);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        mBuilder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.timer)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setOngoing(true)
                .setOnlyAlertOnce(true);
        super.onCreate();
    }


    //..........................................................

    @SuppressWarnings("deprecation")
    private synchronized void updateNotification() {
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_navigation);
        if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
            contentView.setImageViewResource(R.id.btnPlay, R.drawable.ic_launcher_blackpause_foreground);
        } else {
            contentView.setImageViewResource(R.id.btnPlay, R.drawable.ic_launcher_blackplay_foreground);
        }

        contentView.setTextViewText(R.id.textNotifTime, msToHourMinSec(TimeContainer.getInstance().getElapsedTime()) );

        //..........................................................
        mBuilder.setContent(contentView);

        Intent notificationIntent = new Intent(this, TimePlayer.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, PendingIntent.FLAG_UPDATE_CURRENT, notificationIntent, 0);
        mBuilder.setContentIntent(intent);
        if(isNotificationShowing) {
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.getNotification());
        } else {
            isNotificationShowing = true;
            startForeground(NOTIFICATION_ID, mBuilder.getNotification());
        }
    }
//..........................................................

}

If you have any solutions, please kindly help me. Anyways, thanks in any advance


Solution

  • Finally, after 4 hours, I solved my own problem. Here's the thing, my notification was totally deprecated (XD Sorry). I didn't even notice that before. My app is running on targetSdk 26 and minSdk 23 which mean my code above will not work cause most of the functions are deprecated that's why I have to add some suppress for the minSdk. Last but not least, in the end, I have to add Notification Compat and Notification Channel to call the notification. This is caused by the Builder itself. I used the independent variable with Builder on it, and it supposed to call Notification Compat instead.

    So here's what I've changed so far :

        private NotificationCompat.Builder mBuilder;
                //..................
        NotificationChannel mChannel = new NotificationChannel(notifyID, "name", importance);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mNotificationManager.createNotificationChannel(mChannel);
            }
                //..................
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //..................
                mBuilder.setSmallIcon(R.drawable.timer);
                mBuilder.setContentIntent(intent);
                mBuilder.setChannelId(notifyID);
                mBuilder.build();
            }