【问题标题】:How to open same activity on click of notification in android?如何在android中单击通知时打开相同的活动?
【发布时间】:2019-03-05 07:46:42
【问题描述】:

我想在通知点击时打开一个活动让我们说SplashActivity,而不考虑click_action

目前,通过通知,我会收到 click_action 来处理 UI,例如

如果用户在个人资料页面上并且后端团队从管理控制台更新了他的个人资料。
在这种情况下,应用会收到带有 click_action profile_update 的通知。应用会检查 click_action 并刷新个人资料页面。

到目前为止,一切都很好,但是当应用程序在后台并收到带有 click_action profile_update 的通知并且用户单击通知时 - 通知从通知托盘中消失并且没有执行任何导航。

我已声明SplashScreen 将在点击通知时打开,如下所示:

  <activity
            android:name=".SplashScreen"
            android:screenOrientation="portrait">
            <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" />
        </activity>

用户仍然没有导航到SplashScreen

【问题讨论】:

  • 请发布您的句柄通知代码。
  • 所以您想在应用处于后台时执行相同的操作?
  • 嗨@YogeshKatkar。一些你会发现有用的格式化/写作技巧。 (1) 最好使用双输入来创建段落分隔符,因为双&lt;br&gt; 具有不同的语义含义;(2) 人称代词是大写 I,无一例外;(3) 问题最好不要健谈和对话材料,以便由此产生的问题可能对未来的读者有用; (4) 使用反引号的代码格式化通常最好保留给代码和 IO,而不是通用的荧光笔;(5) 请使用像 Grammarly 这样的拼写检查器。 StackOverflow 有自己的方式来格式化文本。

标签: java android firebase


【解决方案1】:

在创建通知时传递活动意图

NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);    
Intent intent = new Intent(this, SplashActivity .class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Prayer time")
                    .setContentText("Its " + salatName + " time.")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pIntent)**Pending intent has the intent of activity you want to open **
                    .addAction(R.drawable.close,"Dismiss",snoozePendingIntent)
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(0, builder.build());

在您希望点击通知时打开的待处理意图中传递 Activity 的意图

【讨论】:

  • 感谢@Bilwal 的回答。仅当payload中未提及click_action时才有效,但如果payload有click_action则无效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多