【问题标题】:Push Notification always Start Main Activity Intent推送通知始终启动主要活动意图
【发布时间】:2017-12-01 15:31:45
【问题描述】:

我的 Android 应用程序中有 3 个活动:

1. Main
2. Welcome
3. Articles

app 的生命周期从 Main -> Welcome -> Article 开始。我已经在我的应用程序中实现了 Firebase 云消息传递 (FCM),我想通过它开始欢迎或文章活动。代码如下:

private void createNote(String title, String body, String key) {
        try {
            Intent screen;
            int code = key == null ? 0 : Integer.parseInt(key);
            if (key == null) {
                screen = new Intent(this, Welcome.class);
            } else {
                screen = new Intent(this, Articles.class);
                screen.putExtra("key", key);
                screen.putExtra("name", title);
            }
            screen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            screen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent intent = PendingIntent.getActivity(
                    this, 0, screen, PendingIntent.FLAG_CANCEL_CURRENT);
            //
            NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setTicker(title + " has new Item")
                    .setSmallIcon(R.drawable.logo)
                    .setContentIntent(intent)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(code, notification.build());
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG + "NOTE", ex.getMessage());
        }
    }

清单文件中的应用程序块

<application
            android:allowBackup="true"
            android:icon="@mipmap/logo"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".infotropy.Main" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".infotropy.Welcome"
                  android:screenOrientation="portrait"
                  android:launchMode="singleInstance"/>
        <activity android:name=".infotropy.Articles" android:screenOrientation="portrait"/>
</application>

我的问题是,无论持有哪个Activity Class Reference screen 变量,点击Push Notification 后,总是Main Activity 启动,我什至在所述函数的任何地方都没有提到。

代码应该启动 WelcomeArticles 活动,但它总是激活 Main。请给出解决方案。

编辑:

按照要求,我正在分享处理 FCM 推送通知的类代码:

public class Notify extends FirebaseMessagingService {

    private final String TAG = "NOTIFY_";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //super.onMessageReceived(remoteMessage);
        try {
            String key = null;
            Map data = remoteMessage.getData();
            String body = remoteMessage.getNotification().getBody();
            String title = remoteMessage.getNotification().getTitle();
            Log.d(TAG, "From : " + remoteMessage.getFrom());
            Log.d(TAG, "Body : " + body);
            if (data.containsKey("key")) {
                key = data.get("key").toString();
            }
            createNote(title, body, key);
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG + "RECEIVER", ex.getMessage());
        }
    }

    private void createNote(String title, String body, String key) {
        try {
            Intent screen;
            int code = key == null ? 0 : Integer.parseInt(key);
            if (key == null) {
                screen = new Intent(this, Welcome.class);
            } else {
                screen = new Intent(this, Articles.class);
                screen.putExtra("key", key);
                screen.putExtra("name", title);
            }
            screen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            screen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            Intent welcome = new Intent(this, Welcome.class);
//            welcome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent intent = PendingIntent.getActivity(
                    this, 0, screen,
                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
            //
            NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setTicker(title + " has new Item")
                    .setSmallIcon(R.drawable.logo)
                    .setContentIntent(intent)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(code, notification.build());
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG + "NOTE", ex.getMessage());
        }
    }
}

【问题讨论】:

  • @VihangaYasith。因为文章活动具有从服务器同步最新数据的机制。因此,每当推送通知出现引用文章时,所有旧活动都应清除,文章将有一个新实例来同步来自服务器的最新数据
  • 分享你的FCM Message Handler的完整代码。
  • @AGMTazim。我已经添加了完整的代码。请看一下

标签: android android-intent push-notification firebase-cloud-messaging android-pendingintent


【解决方案1】:

请试试这个:

Intent intent = new Intent(this, Welcome.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.new_logo)
        .setContentTitle(title)
        .setContentText(message)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setColor(getResources().getColor(R.color.colorPrimary))
        .setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

【讨论】:

  • 不工作。还是一样的问题。除了欢迎或文章之外,主要活动正在启动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多