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?
Asked
Active
Viewed 1,152 times
0
-
http://stackoverflow.com/a/8351704/233048 – mach Sep 09 '14 at 05:56
-
Atleast reply..... have you got the answer or not? – Tarun Sharma Sep 10 '14 at 18:52
-
i am using a transparent activity for showing dialog. we can also use activity theme as dialog for doing the same. – Garjpreet Singh Sep 11 '14 at 03:52
1 Answers
1
There are two ways according to me.
- On Click of Notification, Open a dialog box or dialog activity of requirement.
- 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:

Mete
- 5,495
- 4
- 32
- 40

Tarun Sharma
- 824
- 1
- 11
- 24
-
Please take the time to distinguish your commentary from your code blocks. – ChiefTwoPencils Sep 14 '14 at 09:32