Push Notification Tutorial

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Push Notification Tutorial

Postby nickmoi451 » Mon Apr 08, 2013 4:32 am

Create a notification in Lastest Event Layout
notification_lastest_layout

Image


Code: Select all
private void pushLastestEventNotification() {
            String ns = Context.NOTIFICATION_SERVICE;
            mNotificationManager = (NotificationManager) getSystemService(ns);
            int icon = R.drawable.icon;
            CharSequence tickerText = "Push Notification Tutorial";
            long when = System.currentTimeMillis();
            // Create new Notification objects
            Notification notification = new Notification(icon, tickerText, when);
            // Setting up for Notification fields.
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.number += 1;
            // Create a pending intent to launch to another application when click
            // on notification.
            Context context = getApplicationContext();
            Intent notificationIntent = new Intent(this, ResultActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            // Sets the contentView field to be a view with the standard
            // "Latest Event" layout.Uses the icon and when fields to set the icon
            // and time fields in the view.
            notification.setLatestEventInfo(context, "ContentTitle-9Android.net",
                    "ContentText-Push Notification Tutorial", contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID1, notification);
     
        }




Create a notification in custom layout
Image

Code: Select all
private void pushCustomLayoutNotification() {
            String ns = Context.NOTIFICATION_SERVICE;
            mNotificationManager = (NotificationManager) getSystemService(ns);
            int icon = R.drawable.icon;
            CharSequence tickerText = "Push Notification Tutorial";
            long when = System.currentTimeMillis();
            // Create new Notification objects
            Notification notification = new Notification(icon, tickerText, when);
            // Setting up for Notification fields.
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.number += 1;
            // Create a pending intent to launch to another application when click
            // on notification.
            Context context = getApplicationContext();
            Intent notificationIntent = new Intent(this, ResultActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            // Inflate custom layout to Remote View of Notification object.
            RemoteViews contentView = new RemoteViews(getPackageName(),
                    R.layout.notification_layout);
            contentView.setImageViewResource(R.id.notification_layout_icon_img,
                    R.drawable.icon);
            contentView.setTextViewText(R.id.notification_layout_text,
                    "Notification Tutorial of 9Android.net");
            notification.contentView = contentView;
            notification.contentIntent = contentIntent;
            mNotificationManager.notify(NOTIFICATION_ID2, notification);
     
        }


Download source : http://www.dl.9android.net/index.php?ac ... 1362354679
Source: http://www.9android.net/push-notification/
nickmoi451
Freshman
Freshman
 
Posts: 6
Joined: Mon Apr 08, 2013 2:12 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests