我正在创建一个应用程序,它将接收GCM通知并将其显示为对话框。有没有办法实现代码在Intent Service中显示对话框?
答案 0 :(得分:1)
根据我的说法有两种方式。
打开对话
/**
* 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());
}
大型通知
// 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
}
检查通知上的Google文档: