How to display notification at a particular time

Daniyal Akbar

please tell what i can add to the below code to display notification at a particular time

public class MainActivity extends Activity {

        private WebView wv1;
        Context context;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            wv1 = (WebView) findViewById(R.id.YahooWhether);


            wv1.getSettings().setLoadsImagesAutomatically(true);
            wv1.getSettings().setJavaScriptEnabled(true);
            wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv1.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "here u go", Toast.LENGTH_SHORT).show();
                }
            });
            wv1.loadUrl("https://www.yahoo.com/news/weather/pakistan/sindh/karachi-2211096/");

            sendNotification();



        }
        public void sendNotification() {

    //Get an instance of NotificationManager//

            Notification.Builder mBuilder =
                    new Notification.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setContentTitle("Whether Update")
                            .setContentText("Tomorrow's Forecast");


    // Gets an instance of the NotificationManager service//

            NotificationManager mNotificationManager =

                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            mNotificationManager.notify(001, mBuilder.build());

        }

    }

Ive displayed notification by default but not time specific. Please help. i cant get how to add alarmanager or whatever class that needs to be addded in order to display notification.

Thorny84

You need one AlarmManager class
It is not complicated, this is a reference:
https://developer.android.com/reference/android/app/AlarmManager.html
this is a pratice:
https://developer.android.com/training/scheduling/alarms.html
More or less your code i would write it like this:

public class MainActivity extends Activity {

        private WebView wv1;
        Context context;
        AlarmManager alarmManager;
        Intent notificationAlert;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            wv1 = (WebView) findViewById(R.id.YahooWhether);


            wv1.getSettings().setLoadsImagesAutomatically(true);
            wv1.getSettings().setJavaScriptEnabled(true);
            wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv1.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "here u go", Toast.LENGTH_SHORT).show();
                }
            });
            wv1.loadUrl("https://www.yahoo.com/news/weather/pakistan/sindh/karachi-2211096/");

            alarmManager = ( AlarmManager ) context.getSystemService( Context.ALARM_SERVICE ); //request alarm service
            notificationAlert = new Intent( context, sendNotification.class ); //intent to connection at broadcastReceiver class
            PendingIntent pendingIntent = PendingIntent.getBroadcast( context, 0, notificationAlert, PendingIntent.FLAG_UPDATE_CURRENT ); //request broadcast in AlertNotification class
            try{
                alarmManager.setRepeating(
                        AlarmManager.RTC_WAKEUP, //alarm time in system
                        0, //start milliseconds
                        1000, //difference time to go
                        pendingIntent
                );
            }
            catch( ParseException e ){
                e.printStackTrace();
            }

        }

    }

I use AlarmManager.setReapeating To repeat the Alarm until you do AlarmManager.cancel( pendingIntent );
Otherwise you can use it AlarmManager.set

make new class notification:

public class sendNotification extends BroadcastReceiver {

    @Override
    public void onReceive( Context context, Intent intent ){

    Drawable icon = context.getDrawable(R.mipmap.ic_launcher_round);

    //Get an instance of NotificationManager//

            NotificationCompat.Builder mBuilder =
                    new Notification.Builder(context)
                            .setSmallIcon(icon)
                            .setContentTitle("Whether Update")
                            .setContentText("Tomorrow's Forecast");


    // Gets an instance of the NotificationManager service//

            NotificationManager mNotificationManager =

                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


            mNotificationManager.notify(001, mBuilder.build());

    }

        }

Into AndroidManifest.xml add

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

in application tag

Let me know if there are any problems.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to decrease running time of this particular solution?

From Dev

How to make a function run for a particular time period?

From Dev

How to schedule a request at a particular time?

From Dev

show a notification on a particular date and time

From Dev

How to display list below particular item selected?

From Dev

how to display particular value in dropdown in php?

From Dev

how to trim a video in swift for a particular time

From Dev

How to display a system notification in Python?

From Dev

How to display multiple notification as a group?

From Dev

How to check whether a time is between particular range?

From Dev

How to update time of an Android notification

From Dev

how to display particular array combination?

From Dev

How to display notification every week?

From Dev

How to display multiple notification at the same time

From Dev

How to display Android push notification with no title/time on 3 lines

From Dev

How to send a Notification at a specific time?

From Dev

Android: how to display notification text

From Dev

Display a local notification at a fixed time, ios?

From Dev

Issue in display particular Notification message in my application

From Dev

How to display Tweets of a particular user in an Android app

From Dev

how to open particular fragment when notification is clicked

From Dev

Execute a script every time a particular application makes a notification

From Dev

How to display list below particular item selected?

From Dev

How to send the Firebase notification on particular time?

From Dev

How to display no value for particular legend in amchart?

From Dev

How to set local notification for only particular date with time 12PM and 6PM

From Dev

How to compare stored date and time with SimpleDateFormat and display notification?

From Dev

How to call a web service daily on particular time using local notification in objective c?

From Dev

How to repeat local notification once fired at a particular time

Related Related

  1. 1

    How to decrease running time of this particular solution?

  2. 2

    How to make a function run for a particular time period?

  3. 3

    How to schedule a request at a particular time?

  4. 4

    show a notification on a particular date and time

  5. 5

    How to display list below particular item selected?

  6. 6

    how to display particular value in dropdown in php?

  7. 7

    how to trim a video in swift for a particular time

  8. 8

    How to display a system notification in Python?

  9. 9

    How to display multiple notification as a group?

  10. 10

    How to check whether a time is between particular range?

  11. 11

    How to update time of an Android notification

  12. 12

    how to display particular array combination?

  13. 13

    How to display notification every week?

  14. 14

    How to display multiple notification at the same time

  15. 15

    How to display Android push notification with no title/time on 3 lines

  16. 16

    How to send a Notification at a specific time?

  17. 17

    Android: how to display notification text

  18. 18

    Display a local notification at a fixed time, ios?

  19. 19

    Issue in display particular Notification message in my application

  20. 20

    How to display Tweets of a particular user in an Android app

  21. 21

    how to open particular fragment when notification is clicked

  22. 22

    Execute a script every time a particular application makes a notification

  23. 23

    How to display list below particular item selected?

  24. 24

    How to send the Firebase notification on particular time?

  25. 25

    How to display no value for particular legend in amchart?

  26. 26

    How to set local notification for only particular date with time 12PM and 6PM

  27. 27

    How to compare stored date and time with SimpleDateFormat and display notification?

  28. 28

    How to call a web service daily on particular time using local notification in objective c?

  29. 29

    How to repeat local notification once fired at a particular time

HotTag

Archive