63

我正在尝试使用新的通知界面。我在通知中添加了 3 个按钮,我想在单击每个按钮后将某些内容保存到我的数据库中。

通知本身运行良好,并在调用时显示,我只是不知道如何捕获三个不同按钮单击中的每一个。

我正在使用 aBroadcastReceiver来捕捉点击,但我不知道如何判断点击了哪个按钮。

这是AddAction(我排除了通知的其余部分,因为它运行良好)的代码 -

    //Yes intent
    Intent yesReceive = new Intent();  
    yesReceive.setAction(CUSTOM_INTENT);
    Bundle yesBundle = new Bundle();            
    yesBundle.putInt("userAnswer", 1);//This is the value I want to pass
    yesReceive.putExtras(yesBundle);
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

    //Maybe intent
    Intent maybeReceive = new Intent();  
    maybeReceive.setAction(CUSTOM_INTENT);
    Bundle maybeBundle = new Bundle();            
    maybeBundle.putInt("userAnswer", 3);//This is the value I want to pass
    maybeReceive.putExtras(maybeBundle);
    PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

    //No intent
    Intent noReceive = new Intent();  
    noReceive.setAction(CUSTOM_INTENT);
    Bundle noBundle = new Bundle();            
    noBundle.putInt("userAnswer", 2);//This is the value I want to pass
    noReceive.putExtras(noBundle);
    PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

这是BroadcastReceiver-的代码

 public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("shuffTest","I Arrived!!!!");
     //Toast.makeText(context, "Alarm worked!!", Toast.LENGTH_LONG).show();

    Bundle answerBundle = intent.getExtras();
    int userAnswer = answerBundle.getInt("userAnswer");
    if(userAnswer == 1)
    {
        Log.v("shuffTest","Pressed YES");
    }
    else if(userAnswer == 2)
    {
        Log.v("shuffTest","Pressed NO");
    }
    else if(userAnswer == 3)
    {
        Log.v("shuffTest","Pressed MAYBE");
    }

}           
}

我已经BroadcastReceiver在 Manifest 中注册了。另外,我想提一下,BroadcastReceiver当我单击通知中的一个按钮时会调用它,但意图总是包含额外的“2”。

这是通知iteslf - 通知

4

5 回答 5

117

这是因为您将FLAG_UPDATE_CURRENT与具有相同操作的 Intent 一起使用

从文档:

如果描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换其额外数据。

当您指定pendingIntentMaybeandpendingIntentNo时,系统使用PendingIntent创建的 for pendingIntentYes,但它会覆盖 extras。因此,所有三个变量都指向同一个对象,并且最后指定的附加项是 for pendingIntentNo

您应该为每个Intent. 您仍然可以拥有一个BroadcastReceiver,并让它拦截所有三个动作。这在语义上也不会那么混乱:)

您的通知海报:

//Yes intent
Intent yesReceive = new Intent();  
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

//Maybe intent
Intent maybeReceive = new Intent();  
maybeReceive.setAction(MAYBE_ACTION);
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

//No intent
Intent noReceive = new Intent();  
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

您的接收器:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if(YES_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed YES");
    } else if(MAYBE_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed NO");
    } else if(NO_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed MAYBE");
    }
}           
于 2013-03-20T10:45:09.330 回答
25

一步步

第1步

public void noto2() // paste in activity
{
    Notification.Builder notif;
    NotificationManager nm;
    notif = new Notification.Builder(getApplicationContext());
    notif.setSmallIcon(R.drawable.back_dialog);
    notif.setContentTitle("");
    Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notif.setSound(path);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent yesReceive = new Intent();
    yesReceive.setAction(AppConstant.YES_ACTION);
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
    notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes);


    Intent yesReceive2 = new Intent();
    yesReceive2.setAction(AppConstant.STOP_ACTION);
    PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT);
    notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2);



    nm.notify(10, notif.getNotification());
}

步骤 1.5

我创建了一个全局类 AppConstant

 public class AppConstant 
   {
  public static final String YES_ACTION = "YES_ACTION";
  public static final String STOP_ACTION = "STOP_ACTION";
  } 

第2步:

 public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();
    if (AppConstant.YES_ACTION.equals(action)) {
        Toast.makeText(context, "YES CALLED", Toast.LENGTH_SHORT).show();
    }
    else  if (AppConstant.STOP_ACTION.equals(action)) {
        Toast.makeText(context, "STOP CALLED", Toast.LENGTH_SHORT).show();
    }
}

}

第 3 步

 <receiver android:name=".NotificationReceiver">
        <intent-filter>
            <action android:name="YES_ACTION"/>
            <action android:name="STOP_ACTION"/>

        </intent-filter>
    </receiver>
于 2016-06-12T14:16:03.577 回答
11

在我的情况下,它在添加意图过滤器后对我有用

 <receiver android:name=".AlarmReceiver">
      <intent-filter>
           <action android:name="YES_ACTION"/>
           <action android:name="NO_ACTION"/>
           <action android:name="MAYBE_ACTION"/>
      </intent-filter>
  </receiver>
于 2015-08-09T11:19:13.187 回答
2

这里YES_ACTION一定是yourfullpackagename.YES

private static final String YES_ACTION = "com.example.packagename.YES";

同样,您可以使用NO_ACTIONMAYBE_ACTION

在 BroadcastReceiver 你必须使用与YES_ACTION上面声明的相同,

意味着在 BroadcastReceiver 类中,您可以通过以下方式检查自定义广播

public class NotificationReceiver extends BroadcastReceiver {

private static final String YES_ACTION = "com.example.packagename.YES";
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();
    if(YES_ACTION.equals(action)) {
        Toast.makeText(context, "CALLED", Toast.LENGTH_SHORT).show();
    }
}

}

注意:代替 YES_ACTION 字符串中的 YES,您也可以使用其他词。

于 2015-11-14T10:01:36.537 回答
0

这对我有用

查看示例图片

在您的服务中

private void sendNotification(String title, String body) {

    Intent activityIntent = new Intent(this, HomeActivity.class);
    activityIntent.setAction(Long.toString(System.currentTimeMillis()));

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);

    Intent broadcastIntent = new Intent(this, MyReceiver.class);
    broadcastIntent.putExtra("quotes_copy", body);
    PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);


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

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(NOTI_CHANNEL_ID, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTI_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setContentTitle(title)
            .addAction(R.drawable.ic_logo, "Copy", actionIntent);

    random = new Random();
    NOTIFY_ID = random.nextInt(1000);
    notificationManager.notify(NOTIFY_ID, builder.build());

}

在您的接收器类中

 @Override
public void onReceive(Context context, Intent intent) {

    String message = intent.getStringExtra("quotes_copy");

    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

}

显现

<receiver android:name=".Utils.MyReceiver"/>
于 2021-03-17T05:14:45.480 回答