Android - 向通知添加 Intent

标签 android firebase android-intent notifications

我是 android 开发的新手,但我仍然不太了解 Intent。在这里,我使用 Firebase 创建通知,我向我的通知添加了一个 Intent,但是当我的应用程序在前台并且我点击通知时没有任何反应(当应用程序被终止或在后台时它运行良好)。

奇怪的是,它在某一时刻工作,当我点击通知时,我的“MainActivity”类的函数“onNewIntent”被调用,但现在什么都没有发生,我想我没有改变代码。

以下是我创建 Intent 的方式:

Notifications notifs = new Notifications(this);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String url = remoteMessage.getData().get("url");

Intent intentNotif = new Intent(this, MainActivity.class);
intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);
intentNotif.putExtra("url", url);
intentNotif.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intentNotif.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

notifs.showNotif(title, body, true, intentNotif);

以下是我如何将 Intent 添加到通知中:

this.builderGeneric.setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));

我还尝试使用自定义操作创建一个 Intent ,但它不起作用,我尝试了不同的 Intent 标志,但我觉得我在尝试随机的事情而不知道我在做什么,所以这就是为什么我要求你帮助。

编辑: 以下是我如何创建有用的通知:

Notifications(Context context) {
  this.context = context;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    this.notifManager = context.getSystemService(NotificationManager.class);
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(CHANNEL_DESCRIPTION);
    channel.enableLights(false);
    channel.enableVibration(false);
    channel.setSound(null, null);
    if (this.notifManager != null) {
      this.notifManager.createNotificationChannel(channel);
    }
  } else {
    this.notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  this.builderGeneric = new Notification.Builder(context)
      .setVisibility(Notification.VISIBILITY_PUBLIC)
      .setWhen(System.currentTimeMillis())
      .setSmallIcon(R.mipmap.ic_launcher_round);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    this.builderGeneric.setChannelId(this.CHANNEL_ID);
  }
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
    this.builderGeneric.setSmallIcon(R.mipmap.ic_launcher_foreground);
  }
}

public void showNotif(String title, String text, boolean cancelable, Intent intent) {
  this.builderGeneric.setContentTitle(title)
      .setContentText(text)
      .setOngoing(!cancelable)
      .setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));

  Notification notifGeneric = this.builderGeneric.build();

  this.notifManager.notify(1, notifGeneric);
}

编辑 2:这是我的 list :

<application
  android:allowBackup="true"
  android:fullBackupContent="@xml/backup_descriptor"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:launchMode="standard"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">

  <activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
  </activity>

  <receiver
    android:name=".NetworkChangeReceiver"
    android:label="NetworkChangeReceiver">
    <intent-filter
      android:name=".NetworkIntentFilter"
      android:label="NetworkIntentFilter">
      <action
        android:name="android.net.conn.CONNECTIVITY_CHANGE"
        tools:ignore="BatteryLife" />
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
  </receiver>

  <service android:name=".MyFirebaseService"
    android:exported="false">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>

</application>

最佳答案

好的,先尝试删除

intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);

接下来,在 AndroidManifest.xml 中的 Activity 标签内添加 android:launchMode="singleTask"(同时删除 android:launchMode="standard")。

然后再试一次,请注意 launchMode 是 singleTask 并且如果您在打开 MainActivity 时单击您的通知 -> onNewIntent 将被触发(因为 Android 不会再创建此 Activity 的另一个实例)否则 onCreate 将是打电话。

如果有效,我建议您阅读有关 LaundMode 的更多信息 here

关于Android - 向通知添加 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54901501/

相关文章:

android - "IntentReceiver components are not allowed to register to receive intents"尝试确定电池电量时

android - 如何从中心裁剪为 ImageView 设置动画以填充屏幕,反之亦然(facebook 风格)?

java - strValue.toString() 和 strValue +""和 ""+strValue 之间的区别

swift - 将 firebase 数据库中的每个用户名存储到数组变量中?

android - 为什么 GeoFire 的 GeoQuery 的 queryAtLocation 不返回键,即使在给定 KM 内的经纬度?

android - 将用户选择值从一个 Activity 传递到另一个 Activity

java - Android 日历 Intent 完成后执行操作

java - 从 Android 中的 fragment 管理工具栏中的后退按钮

java - Android 中 RuntimeException ("Stub!"的含义

angularjs - 如何在其他表单成功提交后提交第二个表单