2015-11-27 3 views
0

Я нажимаю уведомление от GCM Server всем клиентам. Непрерывно слышите звук и вибрируйте, пока я не вытащу панель уведомлений.GCM Android - непрерывно слышится звук и вибрирует при получении уведомления

Вот мой код:

private static void generateNotification(Context coNtext, Bundle data) 
{ 
    int icon = R.drawable.launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager nm = (NotificationManager) coNtext.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent ni = new Intent(coNtext, MainActivity.class); 
    ni.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(coNtext, 0, ni, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification noti = new NotificationCompat.Builder(coNtext) 
    .setContentTitle(coNtext.getString(R.string.app_name)) 
    .setContentText(data.getString("message")) 
    .setContentIntent(intent) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setSmallIcon(icon) 
    .setWhen(when) 
    .build(); 
    noti.flags = Notification.FLAG_AUTO_CANCEL; 
    nm.notify(0, noti); 
} 

Я просто хочу, пусть это нормально (звук и вибро только один раз).

Как это исправить?

Спасибо.

ответ

1

Используйте следующий код

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

Intent m_intent = new Intent(this,MainAcicity.class); 
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, m_intent, PendingIntent.FLAG_CANCEL_CURRENT); 

NotificationCompat.Builder mBuilder = (Builder) new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name)).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setAutoCancel(true).setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 
mBuilder.setContentIntent(contentIntent); 
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
+0

Это замечательно. Спасибо. Вы спасли мою жизнь. – user3789802

Смежные вопросы