Android GCM推送通知

时间:2014-10-14 11:42:24

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

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

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);      

}

2 个答案:

答案 0 :(得分: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.");

答案 1 :(得分:0)

尝试将标志 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);