Sending Notifications to users

The notification service will be used whenever an event is nearing by, to promote it and remind all the users of the application about the event. This can be done through a number of ways, which are detailed below:

Google Cloud Messaging: As per Google’s documentation “Google Cloud Messaging (acronymed as GCM) is a service that helps developers send data from servers to their Android apps”. Using this service you can send data to an application whenever new data is available instead of making requests to server in a timely fashion. Integrating GCM in your android application enhances user experience most of all, apart from saving loads of battery consumption. The servers can be of any kind, however, Android tends to work best with the PHP servers.

Android Notification Service: A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time. Notifications, as an important part of the Android user interface, have their own design guidelines. The material design changes introduced in Android 5.0 (API level 21) are of particular importance.

In my app, I have decided to implement the second method.

Below is the code which sets up a new notification:

public class NotificationView extends Activity {
@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.notification);
 }
}

Once this is done, the next step would be to create another class which extends the Broadcast receiver:

public class NotificationEvent extends BroadcastReceiver{
 private int notifID = 100;
 private int notifNum = 0;
 private NotificationManager notifManager;
@Override
 public void onReceive(Context context, Intent intent) {
 popupNotif(context);
}
protected void popupNotif(Context context) {
 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, NotificationEvent.class), 0);
 NotificationCompat.Builder evtNotif = new NotificationCompat.Builder(context);
 evtNotif.setContentTitle("TiBi Event");
 evtNotif.setContentText("");
 evtNotif.setTicker("");
 evtNotif.setSmallIcon(R.drawable.ic_launcher);
 evtNotif.setDefaults(Notification.DEFAULT_SOUND);
 evtNotif.setAutoCancel(true);
 evtNotif.setNumber(++notifNum);
 Intent result = new Intent(context, NotificationView.class);
 TaskStackBuilder stack = TaskStackBuilder.create(context);
 stack.addParentStack(NotificationView.class);
 stack.addNextIntent(result);
 evtNotif.setContentIntent(contentIntent);
 notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 notifManager.notify(notifID, evtNotif.build());
 }
protected void cancelNotif() {
 notifManager.cancel(notifID);
}
}

Once this is completed, the final step would be to call the event notifier in the main activity to display it to the users. This is done through this code:

Intent intent = getIntent();
 String date = intent.getStringExtra(EventFragment.DATE);
 String time = intent.getStringExtra(EventFragment.TIME);
long _date = Long.parseLong(date);
 SimpleDateFormat year = new SimpleDateFormat("yyyy");
 SimpleDateFormat month = new SimpleDateFormat("MMM");
 SimpleDateFormat day = new SimpleDateFormat("dd");
long _time = Long.parseLong(time);
 SimpleDateFormat hour = new SimpleDateFormat("hh");
 SimpleDateFormat minute = new SimpleDateFormat("mm");
year.format(new Date(_date));
 month.format(new Date(_date));
 day.format(new Date(_date));
 hour.format(new Date(_time));
 minute.format(new Date(_time));
int yyyy = Integer.valueOf(String.valueOf(year));
 int MM = Integer.valueOf(String.valueOf(month));
 int dd = Integer.valueOf(String.valueOf(day));
 int mm = Integer.valueOf(String.valueOf(minute));
 int hh = Integer.valueOf(String.valueOf(hour));
cal = Calendar.getInstance();
 cal.set(Calendar.HOUR_OF_DAY, hh - 1);
 cal.set(Calendar.MINUTE, mm);
 cal.set(Calendar.DATE, dd);
 cal.set(Calendar.MONTH, MM);
 cal.set(Calendar.YEAR, yyyy);
Intent notifEvent = new Intent(this, NotificationEvent.class);
 PendingIntent pi = PendingIntent.getService(this, 0 , notifEvent, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.