【问题标题】:GCM Notification onclick does not open specified activityGCM 通知 onclick 未打开指定的活动
【发布时间】:2016-06-06 08:25:32
【问题描述】:

不幸的是,之前提出的问题都没有解决我的问题。我的应用程序坚持不打开指定的活动。相反,它会打开主要活动,即 GCMService.java

private void sendNotification(String message) {
    Intent intent = new Intent(this, LaunchActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT
            | PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("GCM Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

以下是我在 Android 清单中的 LaunchActivity 定义和 GCMService 活动定义

<activity
    android:name=".LaunchActivity"
    android:label="@string/title_activity_launch"
    android:theme="@style/AppTheme.NoActionBar"
    android:exported="true"
    android:launchMode="singleTop">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
    android:name=".GCMService"
    android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

提前致谢。

【问题讨论】:

  • LaunchActivity 中移除这个 来自清单文件。
  • GCMService 类是否使用 Activity 扩展?
  • 从 AppCompatActivity 扩展

标签: android push-notification notifications google-cloud-messaging gcmlistenerservice


【解决方案1】:
 Intent intent = new Intent(this, LaunchActivity.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.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多