在 android studio 单击推送通知后重定向到特定 activity

Redirect to a specific activity upon push notification is clicked on android studio

我正在努力开发一个 Android 应用程序,以便在单击所有传入推送通知时将应用程序重定向到特定的 activity 页面。我是 android 开发的新手,对我的编程感到抱歉。

下面是androidmanifest.xml的推送通知部分。如果对此主题有任何评论,我们将不胜感激:-

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<permission
    android:name="com.younginnovators.sjk.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<!-- VERY IMPORTANT! Don't forget this permission, or in-app billing won't work. -->

<uses-permission android:name="com.younginnovators.sjk.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- Push Notifications Receiver -->
    <receiver
        android:name=".backgroundservices.PushNotificationBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.younginnovators.sjk" />
        </intent-filter>
    </receiver>
    <!-- /Push Notifications Receiver -->


    <!-- Push Notifications Service -->
    <service android:name=".backgroundservices.PushnotificationService" />
    <!-- /Push Notifications Service -->

你可以试试这样的

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);