【问题标题】:Notification onClick点击通知
【发布时间】:2015-09-25 04:30:02
【问题描述】:

感谢我的另一个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);

【问题讨论】:

    标签: java android android-notifications


    【解决方案1】:

    没有活动就不可能有正常的对话框。有几种可能的解决方法,包括 styling the activity like a dialog 和使活动本身不可见并立即从中启动对话框。

    【讨论】:

    • 那么当我点击我的通知时如何决定启动哪个活动?
    【解决方案2】:

    您或许可以使用Remote Views 来显示一个对话框,而无需进入您的应用进程。

    【讨论】:

      【解决方案3】:

      你可以设置

       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); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 2017-03-05
        相关资源
        最近更新 更多