Local push notification in android not showing when app is closed from multitasking window

363 views Asked by At

I am trying to get a local push notification even when the app is closed from multitasking window. I have searched for many tutorials as well as video tutorials to get this working but still haven't been able to figure it out. The push notification works while the app is open or is in the background but won't show when the app is closed. Please let me know what i'm doing wrong.
This is the code in my MainActivity

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);

Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT );

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

This is my code in NotificationReceiver class

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeatingIntent = new Intent(context, MainActivity.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Daily Attendance")
            .setContentText("Hey there this is a notfication")
            .setAutoCancel(true)
            .setSound(alarmSound);
    notificationManager.notify(100, builder.build());
}

My Manifest file looks somewhat like this:

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

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>


    <application
        android:name=".app.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!--broadcase receiver-->
        <receiver android:name=".utils.NotificationReceiver"/>
    </application>

</manifest>

Please tell me what i'm doing wrong and what needs to be done to achieve local push notification even when the app is closed. Thanks in advance :)

0

There are 0 answers