Here is something close to what you need in your main business:
Intent notificationIntent = new Intent(context, ShowNotification.class); PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.cancel(contentIntent); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent);
Then in another file called ShowNotification.java add the following (if your main activity is called MainActivity):
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.util.Log; public class ShowNotification extends Service { private final static String TAG = "ShowNotification"; @Override public void onCreate() { super.onCreate(); Intent mainIntent = new Intent(this, MainActivity.class); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification noti = new NotificationCompat.Builder(this) .setAutoCancel(true) .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT)) .setContentTitle("HELLO " + System.currentTimeMillis()) .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS") .setDefaults(Notification.DEFAULT_ALL) .setSmallIcon(R.drawable.ic_launcher) .setTicker("ticker message") .setWhen(System.currentTimeMillis()) .build(); notificationManager.notify(0, noti); Log.i(TAG, "Notification created"); } @Override public IBinder onBind(Intent intent) {
scottt
source share