Notifications

I am going to add a notification to my application. This will place an icon in the phone's status bar.
First I need an icon. This link contains some very handy design guidelines for Android apps. The table just after the start gives details of icon resolutions for different screen densities. I am just going to create a single icon here. I will look at multiple screen densities later. I am less interested now in the design of the icon than I am in how the notification framework operates, so just a simple red box with a dot in it for now.
Not the most beautiful thing in the world, but it will do for now. 
First up I am going to put a button on an Activity. The onClick will create the notification:

    private void setupNotify() {
        int icon = R.drawable.notify; // icon from resources
        CharSequence tickerText = "Hello"; // this is the text that displays on
        // the status bar when the
        // notification is first displayed.
        // The text of an SMS for example.
        long when = System.currentTimeMillis(); // This is the time that will be
        // displayed beside the
        // notification text when you
        // expand the notification bar.
        // It is typically just the
        // current time.
        Context context = getApplicationContext();

        CharSequence contentTitle = "My notification"; // the large text
                                                        // displayed beside the
                                                        // icon on the expanded
                                                        // notification page.
        CharSequence contentText = "Hello World!"; // Smaller text beneath the
                                                    // title.

        Intent notificationIntent = new Intent(this, ResultsActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        // Create the notification with the simple text passed in.
        Notification notification = new Notification(icon, tickerText, when);

        // This method finishes up the setup of the notification. It is not used
        // for RemoteViews. They have custom layouts set.
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);

        // Finally get a reference to the NotificationManager and call the
        // notify method.
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.notify(MY_NOTIFICATION_ID, notification);

    }



Comments

Popular posts from this blog

Building a choropleth map for Irish agricultural data

Early Stopping with Keras

AutoCompleteTextView backed with data from SQLite in Android