【问题标题】:Android GCM push notificationAndroid GCM 推送通知
【发布时间】:2014-10-14 11:42:24
【问题描述】:

我的推送通知有问题,当我点击通知时,没有新的意图开始。 请帮我解决这个问题, 提前致谢。

private static void generateNotification(Context context,String htitle, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    //String title = context.getString(R.string.app_name);
    String title=htitle;
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("head",title);
    notificationIntent.putExtra("desc",message);
    // set intent so it does not start a new activity
    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;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      

}

【问题讨论】:

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


【解决方案1】:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("head",title);
notificationIntent.putExtra("desc",message);

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

在清单文件中

<activity
    android:name="com.android.testing.MainActivity"
   android:noHistory="true">
</activity>

在我的应用程序中我使用了这个希望,这会帮助你

mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("App Name")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    Log.d(TAG, "Notification sent successfully.");

【讨论】:

  • Vaishali,试过了,但还是一样,没有变化:(
  • 非常感谢 Vaishali,我会试试这个。
【解决方案2】:

尝试将标志 Intent.FLAG_ACTIVITY_NEW_TASK 设置为 notificationIntent

PendingIntent.FLAG_UPDATE_CURRENT 到 PendingIntent

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent  Intent = PendingIntent.getActivity(this, iUniqueId, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 嗨,约翰,那个 iUniqueId 是什么?
  • IUniqueId 只会在创建通知时创建唯一 id,在您的情况下,它的变量“when”但您没有使用它
猜你喜欢
  • 2016-07-24
  • 2015-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多