Ghadeer
Ghadeer

Reputation: 638

Send Notification based on time Android

I am new to Android development, and I need to send a notification based on 2 cases:

  1. On "showNotification" Button Click using "showNotification" Method
  2. After a period of time (5 seconds after "alertNotification" button clicked in my example) using "alertNotification" method as below:

The first case has worked perfect. But, I am having a problem with the Second case.

I followed the same way explained on https://youtu.be/gm5n_hRIR-c

Here are my Code:

  1. MainActivity.java

    public class MainActivity extends Activity {
        // Send Notification on button click
        Button showNotification;
        // Send Notification 5 second after on button click
        Button alertNotification;
        NotificationManager notificationManager;
        boolean isNotificActive = false;
    
        int notifID = 33;
    
        public static final int NOTIFICATION_ID = 1;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sample_layout);
    
            showNotification = (Button) findViewById(R.id.showNotification);
            alertNotification = (Button) findViewById(R.id.alerNotification);
        }
    
        public void showNotification(View view) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle("Notification")
                    .setContentText("New Notification")
                    .setTicker("Alert New Notification")
                    .setSmallIcon(R.drawable.ic_stat_notification);
    
            Intent moreInfoIntent = new Intent(this, MoreNotification.class);
            TaskStackBuilder stackBuilder = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                stackBuilder = TaskStackBuilder.create(this);
    
                stackBuilder.addParentStack(MoreNotification.class);
                stackBuilder.addNextIntent(moreInfoIntent);
    
                PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.    FLAG_UPDATE_CURRENT);
    
                notificationBuilder.setContentIntent(pendingIntent);
    
                notificationManager = (NotificationManager) getSystemService(Context.    NOTIFICATION_SERVICE);
    
                notificationManager.notify(notifID, notificationBuilder.build());
    
                isNotificActive = true;
            }
        }
    
        public void alertNotification(View view) {
            Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
            Intent alertIntent = new Intent(this, AlertReceiver.class);
    
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
            alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(
                    this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    
            Log.e("alarmManager", "alarmManager");
        }
    }
    
  2. AlertReceiver.java

    public class AlertReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("onReceive", "onReceive");
            createNotification(context, "Times UP", "5 Seconds Has Passed", "Alert");
        }
    
        public void createNotification(Context context, String msg, String msgText, String msgAlert) {
            PendingIntent notificationIntent = PendingIntent.getActivity(context, 0,
                    new Intent(context, MainActivity.class), 0);
    
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_notification)
                    .setContentTitle(msg)
                    .setTicker(msgAlert)
                    .setContentText(msgText);
    
            mBuilder.setContentIntent(notificationIntent);
            mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
            mBuilder.setAutoCancel(true);
    
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(      Context.NOTIFICATION_SERVICE);
    
            mNotificationManager.notify(1, mBuilder.build()); 
        }
    }
    
  3. AndroidManifest.xml

    <!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
    
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <activity
            android:name=".MoreNotification"
            android:label="More Notification"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
    
    
        </activity>
    </application>
    

Upvotes: 0

Views: 175

Answers (1)

Michael
Michael

Reputation: 54725

AlertReceiver must be registered in AndroidManifest.xml.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- Your stuff here. -->

    <receiver android:name=".AlertReceiver"/>

</application>

Upvotes: 1

Related Questions