【问题标题】:Android Notification data is not passed in terminated and background stateAndroid Notification 数据未在终止和后台状态下传递
【发布时间】:2022-08-18 18:04:24
【问题描述】:

我正在研究android推送通知,通知处于所有三种状态,然后,当我点击通知数据时,只会进入前台状态,但不会进入终止和后台。

服务文件中的待处理意图:

val intentVid = Intent(context, MainActivity::class.java)
    intentVid.putExtra(\"Id\", \"787789\")
    intentVid.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
    val pendingIntent = PendingIntent.getActivity(
        context, 0,
        intentVid, PendingIntent.FLAG_UPDATE_CURRENT
    )

下面的函数在 onCreate 和 onNewIntent 函数中被调用:

   private fun handleNotificationEvent(intent: Intent?) {
            if (intent!!.extras != null) {
                order_status = intent.extras!!.getString(\"Id\", \"\")
                if (order_status.length == 6) {
                    Toast.makeText(
                        this,
                        \"ID :- \" + intent.extras!!.getString(\"Id\", \"\"),
                        Toast.LENGTH_LONG
                    )
                        .show()
                }
            }
        }

    标签: android android-studio kotlin push-notification android-notifications


    【解决方案1】:

    您的代码适合通知。

    1. 转到android清单文件并找到启动器活动,如下所示 行示例。

      <activity
        android:name=".ui.splash.SplashActivity"
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      
    2. 然后在启动器活动中使用以下代码,然后将数据传递给您的活动。

      private fun handleNotificationEvent(intent: Intent?) {
      if (intent!!.extras != null) {
          order_status = intent.extras!!.getString("Id", "")
          if (order_status.length == 6) {
              Toast.makeText(
                  this,
                  "ID :- " + intent.extras!!.getString("Id", ""),
                  Toast.LENGTH_LONG
              )
                  .show()
        }
       }
      }
      
    3. 这将有助于在终止和后台情况下从通知中获取数据。

    4. 就我而言,我会将数据放入启动活动中。

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 2017-11-24
      • 2021-04-01
      • 2022-07-14
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      相关资源
      最近更新 更多