0

i am creating an app which will receive GCM notifications and show them as a dialog. is there any way to implement code to show a dialog in Intent Service?

1 Answers1

1

There are two ways according to me.

  1. On Click of Notification, Open a dialog box or dialog activity of requirement.
  2. Use BigStyle Notification but it requires api level above 16

Open your Dialog

/**
 * Issues a notification to inform the user that server has sent a message.
 */
private void generateNotification(Intent intent) {

    int requestID = (int) System.currentTimeMillis(); // Some changes required to work on 4.4 kitkat 

    // Notification Builder 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setContentText("Content of notification")
            .setLargeIcon(
                    BitmapFactory.decodeResource(getResources(),
                            R.drawable.ic_launcher));

    // set default Ringtone 
    mBuilder.setSound(RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    mBuilder.setAutoCancel(true);

    // open yopur dialog activity on Click of notification 
    notificationIntent = new Intent(this, DialogActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // Getting data From GCM
    if (intent.getExtras().getString("data") != null
        // passing data to Dialog
        notificationIntent.putExtra("data", your_string_data);
    }
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Pending intent shouid be PendingIntent.FLAG_UPDATE_CURRENT for data
    PendingIntent pIntent = PendingIntent.getActivity(this, requestID,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager)    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(sNotificationId++, mBuilder.build());
}

Big Style Notification

// know device version
public static final int build =  Build.VERSION.SDK_INT;
if(build >= 16){

    // Big style notifications
    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    Notification noti = new Notification();     
    // Call method and send required intent   
    noti = setBigTextStyleNotification(intent);

    noti.defaults |= Notification.DEFAULT_LIGHTS;
    noti.defaults |= Notification.DEFAULT_VIBRATE;
    noti.defaults |= Notification.DEFAULT_SOUND;

    noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

    mNotificationManager.notify(1, noti);
}


/**
 * Big Text Style Notification
 * 
 * @return Notification
 * @see CreateNotification
 */
private Notification setBigTextStyleNotification(Intent intent) {

    Bitmap remote_picture = null;
    // Get data from intent 
    String trip_date = intent.getExtras().getString("Data");

    // Create the style object with BigTextStyle subclass.
    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle("Title");
    notiStyle.setSummaryText("");// this contain text like Gmail notification 

    try {
        remote_picture = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);// getting icon 

    } catch (Exception e) {
        e.printStackTrace();
    }

    // Add the big text to the style.
    CharSequence bigText = "you have confirmed a new trip."
            + "Add to Calendar";
    notiStyle.bigText(bigText);

    // Creates an explicit intent for an ResultActivity to receive.
    Intent resultIntent = new Intent(this, DashboardActivity.class);

    // This ensures that the back button follows the recommended convention
    // for the back key.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    resultIntent.putExtra("data");
    // Adds the back stack for the Intent (but not the Intent itself).
    stackBuilder.addParentStack(SplashActivity.class);

    // Adds the Intent that starts the Activity to the top of the stack.
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    return new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
            .setLargeIcon(remote_picture)
            .setContentIntent(resultPendingIntent)
            .addAction(R.drawable.yes, "", resultPendingIntent) // setting yes or No images 
            .addAction(R.drawable.cancel, "", resultPendingIntent) //Cancel images 
            .setContentTitle("").setContentText("") //Title and Content 
            .setStyle(notiStyle).build();//Style of notifications 
}

Check Google Docs on notifications:

Notifications

Notifying the User

Mete
  • 4,855
  • 4
  • 26
  • 34
Tarun Sharma
  • 804
  • 1
  • 10
  • 23