简体   繁体   中英

How to repeat notification on specific time in android through service

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM