待定意图或单击通知无法打开我的期望活动

时间:2018-10-03 05:53:19

标签: android notifications android-pendingintent

请找到sendNotfication的代码。我尝试了很多事情,但仍然无法正常工作。我需要更改一些Mainfest文件吗?

private void sendNotification(String messageBody) {
    int requestID = (int) System.currentTimeMillis();
    Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle(getString(R.string.fcm_message))
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(requestID/* ID of notification */, notificationBuilder.build());
}

请找到mainfest文件。这与我在firbase中找到的示例相同。但是仍然当我单击通知时,它总是转到SplashScreen。 我尝试了最近的评论,但仍然无法正常工作。

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:fullBackupContent="@xml/backup_descriptor"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar">
    <activity
        android:name=".SplashScreenActivity"
        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=".HomeActivity"
        android:screenOrientation="portrait" />




    <!-- [START firebase_service] -->
    <service android:name=".FCMTest.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <!-- [END firebase_service] -->
    <service
        android:name=".FCMTest.MyJobService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/abc" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

    <activity android:name="com.example.guptasachin.myapplication.NotificationActivity"></activity>

1 个答案:

答案 0 :(得分:0)

您在许多地方都使用了请求ID。

请检查以下工作代码以打开所需的活动:

在这里<NA>

activity = CurrentActivity context,

尝试以下代码:

   mNotificationId = unique constatnt number like 200,201..e.t.c