点击通知会转到我的电话系统设置,而不是打开应用程序

时间:2017-12-29 04:31:34

标签: android firebase-cloud-messaging android-notifications

我正在使用Firebase功能来执行推送通知,并且效果很好。但是当我点击某种类型的通知收到通知后,我需要手机设置(Wifi设置或应用程序选择器对话框,具体取决于我正在测试的手机),而不是打开应用程序。以下是我的FirebaseMessagingService代码。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d(TAG, "onMessageReceived: url is " + remoteMessage.getData().get("avatar_url"));

    myRef = FirebaseDatabase.getInstance().getReference();


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext(), channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("body"))
            .setLargeIcon(ImageManager.getBitmapFromUrl(remoteMessage.getData().get("avatar_url")));

    type = remoteMessage.getData().get("type");
    userName = remoteMessage.getData().get("title");
    Log.d(TAG, "onMessageReceived: username is " + userName);
    uid = remoteMessage.getData().get("uid");
    Log.d(TAG, "onMessageReceived: uid is " + uid);
    chatId = remoteMessage.getData().get("chatId");
    Log.d(TAG, "onMessageReceived: chatId is " + chatId);
    notificationId = remoteMessage.getData().get("notificationId");
    Log.d(TAG, "onMessageReceived: notification id is " + notificationId);


    if(type.equals("message")){

        Intent resultIntent = new Intent("android.intent.action.MESSAGING");
        resultIntent.putExtra("chatId", chatId);
        resultIntent.putExtra("uid", uid);
        resultIntent.putExtra("userName", userName);

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        getApplicationContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setAutoCancel(true);

        myRef.child("notifications")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(notificationId)
                .setValue(null);

    } else {

        //Getting problem here

        Intent resultIntent = new Intent("android.intent.action.MAIN");
        resultIntent.putExtra("FragmentIndicator", "2");

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        getApplicationContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setAutoCancel(true);

        myRef.child("notifications")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(notificationId)
                .setValue(null);

    }

    int mNotificationId = (int) System.currentTimeMillis();
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

对于这个逻辑if(type.equals("message")),一切正常。但问题在于else语句,其中包括朋友请求或朋友请求确认的通知类型,在这种情况下我打算将用户引导到我的聊天片段。

我对主要活动的表现是这样的:

<activity
        android:name=".HomeActivity"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

任何想法在这里出了什么问题?非常感激。

1 个答案:

答案 0 :(得分:1)

您正在为待处理的意图提供系统级意图,因此它将打开系统设置。 相反,您应该从应用程序清单中定义的应用程序中提供HomeActivity.class或任何其他活动。

你走了。

 if(type.equals("message")){

    Intent resultIntent = new Intent(getApplicationContext(),HomeActivity.class);
    resultIntent.putExtra("chatId", chatId);
    resultIntent.putExtra("uid", uid);
    resultIntent.putExtra("userName", userName);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    getApplicationContext(),
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setAutoCancel(true);

    myRef.child("notifications")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child(notificationId)
            .setValue(null);

} else {

    //Getting problem here

    Intent resultIntent = new Intent(getApplicationContext(),HomeActivity.class);
    resultIntent.putExtra("FragmentIndicator", "2");

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    getApplicationContext(),
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setAutoCancel(true);

    myRef.child("notifications")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child(notificationId)
            .setValue(null);

}