如何点击通知?

时间:2017-03-27 05:10:26

标签: android android-studio android-notifications

我的通知可以显示,但我也希望它可以点击,以便点击它时会打开它来自的相同活动。

public void acceptNotification(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(RequestConfirm.this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("PEOPLE HELPER");
        builder.setContentText("Your request has been accepted");

        Intent intent = new Intent(RequestConfirm.this, BroadcastFragment.class); //creates an explicit intent
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(RequestConfirm.this);
        stackBuilder.addParentStack(RequestConfirm.this); //adds the intent
        stackBuilder.addNextIntent(intent);  //put the intent to the top of the stack
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); //(id, flag) //creates a pending intent
        builder.setContentIntent(pendingIntent); //adds the PendingIntent to the builder
        NotificationManager notificationManager = (NotificationManager) RequestConfirm.this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

5 个答案:

答案 0 :(得分:0)

像这样:

Intent gotoIntent = new Intent();
gotoIntent.setClassName(getApplicationContext(), "FULL CLASS NAME");
PendingIntent contentIntent = null
contentIntent = PendingIntent.getActivity(getApplicationContext(),
                    (int) (Math.random() * 100), gotoIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

现在在通知构建器中设置:

.setContentIntent(contentIntent)

答案 1 :(得分:0)

要开始活动,您必须使用标记Intent.FLAG_ACTIVITY_NEW_TASK

Intent intent = new Intent(this, YourActivityClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
        this, 
        0, 
        intent, 
        Intent.FLAG_ACTIVITY_NEW_TASK);

答案 2 :(得分:0)

调用notificationManager.notify(0,builder.build());当您想要显示通知时。点击该等待处理的意图即可开始。

答案 3 :(得分:0)

试试这个

  Intent intent = new Intent(this,MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
  notificationBuilder.setContentTitle(title);
  notificationBuilder.setContentText(msg);
  notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
  notificationBuilder.setVibrate(vibrationPattern);
  notificationBuilder.setAutoCancel(true);
  notificationBuilder.setContentIntent(pendingIntent);
  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(0,notificationBuilder.build());

答案 4 :(得分:0)

试试这个,希望这对你有所帮助。

NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(CurrentActivity.this);
Notification notify;
PendingIntent pending = PendingIntent.getActivity(CurrentActivity.this, 0,
new Intent(CurrentActivity.this, NextActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notify = builder.setContentIntent(pending)
                 .setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
                 .setSmallIcon(R.drawable.logop).setTicker(excep).setWhen(System.currentTimeMillis())
                 .setAutoCancel(true).setContentTitle("your Notification title")
                 .setContentText(message1).build();

CurrentActivity指的是属于您的服务Activity / class的Activty / class,nextActivity指的是您要移动的Activity / class。