Reputation: 33
I have create a notification panel but there is some problem . I have created a service for this but notification is not repeating. I am not understanding whats problem. Here is my code:
MainActivity.java
public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this , Notification.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20*1000 , pendingIntent);
}
});
}
}
Notification.java
public class Notification extends Service{
@Override
public void onCreate() {
Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(this, "hahaha","U have recived new message", pendingIntent);
notificationManager.notify(9999, notification);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
in AndroidManifest.xml
I declared permission for alarm and declered service.
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<service android:name=".Notification"></service>
there is a problem, when i click on button then service start an notification show.but notification is not repeating.
Upvotes: 0
Views: 1652
Reputation: 7494
You are making the notification using the same id (9999 in your case). So when a new notification is made, all it does is replace the existing notification. So as far you see, only one notification shows up. To see different notifications, you should change the id of your notification in your notify() method.
The other option would be to check if a notification is already displayed (I believe this feature is relatively new) and then update the existing notification to let the user know that a new one has been received.
EDIT: Besides this, you are only creating the notification once because you only initialize the Notification service once. The next time the alarm manager repeats it sees that the service is already started and does not initialize a new instance. You will have to stop the service before the next scheduled period for the alarm manager to initialize it once again.
This is anyway not advisable. If you need notifications to be scheduled repeatedly, construct notification in a background thread in a service at routine intervals within a loop until a certain condition is met.
Upvotes: 1