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.
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.
Comments