推送通知始终启动主要活动意图

时间:2017-12-01 15:31:45

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

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

1. Main
2. Welcome
3. Articles

应用程序的生命周期从Main开始 - >欢迎 - >文章。我已在我的应用程序中实施了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>

我的问题是,无论哪个活动类参考屏幕变量保持,点击推送通知后,始终主要活动开始,我甚至没有在上述功能中提到任何地方。

代码应该从欢迎文章活动开始,但它始终会激活主要。请给出一个解决方案。

修改:

如我所知,我正在分享处理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());
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

请试试这个:

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());