ASHISH GUPTA
ASHISH GUPTA

Reputation: 61

Creating Notifications

I am new to Android Development . I am working on a project that involves creating a "To-do" list where the user can add details of upcoming events. These events are then displayed in a List View. Now I want that on the date of an event a Notification is created & displayed.
I have following questions.

  1. I cannot differentiate whether I should use Services or BroadCastReciver to do so.
  2. There can be multiple "To-Do" things so for each event Notification has to put up. So do I have to create multiple Services (or BroadcastReceivers)?

Upvotes: 1

Views: 65

Answers (2)

shriya
shriya

Reputation: 21

try this method once just pass . this works for me for sending event notification at particular date and time.call the following method.

Calendar calSet = Calendar.getInstance();
                        calSet.setTimeInMillis(System.currentTimeMillis());
                        calSet.clear();
                        calSet.set(Eventyear,Eventmonth,DayEvent,EventTimeHrs,EventTimeMin, 0);

setAlarm(context,calSet);




    private void setAlarm(Context context, Calendar targetCal) {

            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");

            Intent intent = new Intent(context, com.newme.main.AlarmReceiver.class);
            intent.putExtra("title", title);
            intent.putExtra("des", des);
            intent.putExtra("startTime", StartTime);
            intent.putExtra("date", dateStr);
            intent.putExtra("venue", venue);
            intent.putExtra("time", timeStr);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, RQS_1, intent, 0);
            AlarmManager  alarmManager   = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
            alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
        }


public class AlarmReceiver extends BroadcastReceiver {
    int mNotificationId = 001;
    private String des,title,StartTime,date,venue,time;

    @SuppressWarnings({ "deprecation", "static-access" })
    @Override
    public void onReceive(Context context, Intent intent) {

        //Toast.makeText(context, "Show Event", Toast.LENGTH_LONG).show();

        int icon = 0;
        long notificationTime       = 0;

        icon        = R.drawable.app_icon_client;  

        des         =   intent.getStringExtra("des");
        String decStr   =Html.fromHtml(des).toString();
        title       =   intent.getStringExtra("title");
        StartTime   =   intent.getStringExtra("startTime");
        date        =   intent.getStringExtra("date");
        venue       =   intent.getStringExtra("venue");
        time        =   intent.getStringExtra("time");


        notificationTime    = System.currentTimeMillis(); 
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                        );


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context);
        Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(AppConstants.APP_NAME)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Reminder"+" "+title+"\ntomorrow. Hope to see you there."))
                .setContentIntent(resultPendingIntent)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(mNotificationId, notification);

        /**
         * vibration 
         * */
        notification.vibrate=new long[] {100L, 100L, 200L, 500L};

        PushNotificationUtils.notificationReceived=true;
        PowerManager pm     = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        WakeLock wl         = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire();

    }
}

Upvotes: 2

userDroid
userDroid

Reputation: 198

You can simply do it by using AlarmManger. Refer this link to learn how to setup an alarm.

http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/

Then you can customise it with notificationManager to creating notification in AlertDemo class.

Upvotes: 0

Related Questions