【问题标题】:how not to open the app by clicking on the notification?如何不通过点击通知打开应用程序?
【发布时间】:2020-09-11 07:47:30
【问题描述】:

这是我第一次使用通知。

使用这段代码,我可以读取 Log() 中写的内容:

 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    contentView.setTextViewText(R.id.title, "Custom notification");
    contentView.setTextViewText(R.id.text, "This is a custom layout");

    Intent intentDown = new Intent(this, myIntentClass.class);
    intentDown.putExtra("DO", "down");
    PendingIntent pDown = PendingIntent.getActivity(this,0,intentDown,0);
    contentView.setOnClickPendingIntent(R.id.drop_down, pDown);

    Intent intentUp = new Intent(this, myIntentClass.class);
    intentUp.putExtra("DO", "up");
    PendingIntent pUp = PendingIntent.getActivity(this,1,intentUp,0);
    contentView.setOnClickPendingIntent(R.id.drop_up, pUp);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelSmoke")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContent(contentView)
            .setSubText("Sub Text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setOngoing(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(100, builder.build());

这是我的 Intent 类:

public class myIntentClass extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String action = (String) Objects.requireNonNull(getIntent().getExtras()).get("DO");
    assert action != null;
    if (action.equals("down")) {
        Log.d("CLICKED", "DOWN");
        finish();
    }else if(action.equals("up")){
        Log.d("CLICKED"," UP");
        finish();
    }
}
}

但是,当我单击“R.id.drop_down”或“R.id.drop_up”按钮时,应用程序会打开。可以非打开应用程序(如 netflix 通知,使用 chromecast)。

编辑: 我解决了使用接收器而不是 IntentClass 的问题。

【问题讨论】:

  • 请添加并接受您的问题的答案。这将从未回答的问题列表中删除该问题,并可能帮助遇到类似问题的其他人。谢谢!

标签: android android-intent notifications android-pendingintent remoteview


【解决方案1】:

只需使用 Reciever 代替 IntentClass

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多