【问题标题】:Android push notification. App started on main Activity安卓推送通知。应用程序在主 Activity 上启动
【发布时间】:2015-08-20 12:03:15
【问题描述】:

我的 Android 应用程序侦听 GCM 的推送通知。 在消息中,我放置了一个 json 对象,其中包含一些要在应用程序中处理的信息,因此我能够基于此启动不同的活动。 这里是启动 PendingIntent 的代码:

@Override
    public void onMessageReceived(String from, Bundle data) {

...
...
// here i create the pending intent object based on json sent in the notification message from server side
...
...
// add the notification
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(defaultIcon)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_my_icon))
                    .setContentTitle("My Application")
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setVibrate(vibrate)
                    .setContentIntent(pendingIntent);

我不清楚为什么,如果应用程序没有运行,应用程序会在主 Activity 上启动。我想要的是它一直关闭,直到用户点击通知。那可能吗?有办法吗?

谢谢 大卫

【问题讨论】:

    标签: android android-activity google-cloud-messaging android-pendingintent


    【解决方案1】:

    我想要的是它在用户点击通知之前一直关闭。

    应该是可以的。不要给你的“主要活动”一个布局,做你的工作来显示通知,并将一个 Intent for onClick 通知操作放入 通知生成器。之后直接关闭活动。 用另一个活动捕捉这个意图(放另一个 Activity 类进入 Intent),即有一个布局。

    编辑:

    您还可以将透明的应用主题应用于您的主要活动,以避免 弹出黑屏几毫秒。

      <style name="AppTheme.Transparent">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowShowWallpaper">true</item>
      </style>
    

    在你的清单中使用它:

    android:theme="@style/AppTheme.Transparent"
    

    【讨论】:

    • 感谢 JasksOnF1re!这是一个很好的解决方案,我接受它。我在想的是像 Whatsapp messenger 这样的著名应用程序是如何实现它的。事实上,在聊天中有新消息时,应用程序并没有启动。只有当我点击通知时它才会打开。由于我可以在通知消息上“连接”一个特定的活动(有一个特定的方法“setContentIntent”)对我来说听起来很奇怪,我们必须为此实施一个“解决方法”。这对我来说仍然不清楚:-)
    • 我敢打赌他们已经实现了一项服务,该服务在后台监听推送。我不确定,但我认为您可以配置 gcm 以使服务接收推送。
    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多