Android - Notification

> Android

1 - About

Notification in Android

3 - Implementation

3.1 - Resources

3.1.1 - String

In the string.xml,

  • A notification message:
<string	name="format_notification">
    My Notification Message: <xliff:g id="condition">%1$s</xliff:g> 
</string>
<string name="pref_last_notification">last_notification</string>
  • Strings related to Notification Enabled preference
<string name="pref_enable_notifications_key" translatable="false">enable_notifications</string>
<string name="pref_enable_notifications_label">Notifications of myApp</string>
<string name="pref_enable_notifications_true">Enabled</string>
<string name="pref_enable_notifications_false">Not Enabled</string>
<string name="pref_enable_notifications_default" translatable="false">true</string>
Advertising

3.1.2 - pref_general.xml

In pref_general.xml add a new CheckBox preference:

<CheckBoxPreference
 android:title="@string/pref_enable_notifications_label"
 android:key="@string/pref_enable_notifications_key"
 android:summaryOff="@string/pref_enable_notifications_false"
 android:summaryOn="@string/pref_enable_notifications_true"
 android:defaultValue="@string/pref_enable_notifications_default" />

3.2 - Code

This code will:

  • check if the notification are enabled
  • send one notification by day if it's the case.
private static final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; 
private static final int NOTIFICATION_ID = 3004;
 
private void notifyMeOnTimeADay() {
 
	Context context = getContext();
 
	//checking the last update and notify if it' the first of the day
	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
	String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key);
	boolean displayNotifications = prefs.getBoolean(displayNotificationsKey,
			Boolean.parseBoolean(context.getString(R.string.pref_enable_notifications_default)));
 
	if ( displayNotifications ) {
 
			// Checking the last update and notify if it' the first of the day
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			String lastNotificationKey = context.getString(R.string.pref_last_notification);
			long lastSync = prefs.getLong(lastNotificationKey, 0);
 
			if (System.currentTimeMillis() - lastSync >= DAY_IN_MILLIS) {
 
					// The notification properties
					int iconId = R.drawable.my_icon;
					String title = context.getString(R.string.app_name);
					String contentText = String.format(context.getString(R.string.format_notification),"I'm notifying you");
 
					// build your notification here.
					// NotificationCompatBuilder is a very convenient way to build backward-compatible
					// notifications.  Just throw in some data.
					NotificationCompat.Builder mBuilder =
							new NotificationCompat.Builder(getContext())
									.setSmallIcon(iconId)
									.setContentTitle(title)
									.setContentText(contentText);
 
					// Make something interesting happen when the user clicks on the notification.
					// In this case, opening the app is sufficient.
					Intent resultIntent = new Intent(context, MainActivity.class);
 
					// The stack builder object will contain an artificial back stack for the
					// started Activity.
					// This ensures that navigating backward from the Activity leads out of
					// your application to the Home screen.
					TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
					stackBuilder.addNextIntent(resultIntent);
					PendingIntent resultPendingIntent =
							stackBuilder.getPendingIntent(
									0,
									PendingIntent.FLAG_UPDATE_CURRENT
							);
					mBuilder.setContentIntent(resultPendingIntent);
 
					NotificationManager mNotificationManager =
							(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
					// NOTIFICATION_ID allows you to update the notification later on.
					mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
 
					//refreshing last sync
					SharedPreferences.Editor editor = prefs.edit();
					editor.putLong(lastNotificationKey, System.currentTimeMillis());
					editor.commit();
			}
	}
}

Example:

4 - Documentation / Reference

Advertising