2012-10-12 87 views
3

我已成功創建通知,這要歸功於我的其他question,使用NotificationCompat。我想現在只需更改通知在點擊上的作用。我真的只是想彈出一個警告對話框(我看過一些應用程序就這麼做),但每次點擊它時,我都會顯示我的活動。有任何想法嗎?通知onClick

通知碼:

Intent notificationIntent = new Intent(ctx, YourClass.class); 
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 
     YOUR_PI_REQ_CODE, notificationIntent, 
     PendingIntent.FLAG_CANCEL_CURRENT); 

NotificationManager nm = (NotificationManager) ctx 
     .getSystemService(Context.NOTIFICATION_SERVICE); 

Resources res = ctx.getResources(); 
Notification.Builder builder = new Notification.Builder(ctx); 

builder.setContentIntent(contentIntent) 
      .setSmallIcon(R.drawable.some_img) 
      .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img)) 
      .setTicker(res.getString(R.string.your_ticker)) 
      .setWhen(System.currentTimeMillis()) 
      .setAutoCancel(true) 
      .setContentTitle(res.getString(R.string.your_notif_title)) 
      .setContentText(res.getString(R.string.your_notif_text)); 
Notification n = builder.build(); 

nm.notify(YOUR_NOTIF_ID, n); 

回答

2

你不能沒有一個活動正常對話。有幾種可能的解決方法,包括styling the activity like a dialog,並使活動本身不可見並立即從其啓動對話框。

+0

所以,我怎麼能決定推出哪些活動,當我點擊我的通知? – EGHDK

0

您或許可以使用Remote Views來顯示對話框,而無需進入您的應用程序進程。

0

可以設置

Intent intent = new Intent(context, ReserveStatusActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

    NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 





    intent = new Intent(String.valueOf(PushActivity.class)); 
    intent.putExtra("message", MESSAGE); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(PushActivity.class); 
    stackBuilder.addNextIntent(intent); 
    // PendingIntent pendingIntent = 
    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 


    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
      0, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.BigPictureStyle notiStyle = new 
      NotificationCompat.BigPictureStyle(); 

    android.app.Notification notification = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(en_title) 
      .setContentText(en_alert) 
      .setAutoCancel(true) 
      .setNumber(++numMessages) 
      .setStyle(notiStyle) 

      //.setContentIntent(resultPendingIntent) 

        .setContentIntent(pendingIntent) 

      .build(); 

    notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    notificationManager.notify(1000, notification);