簡體   English   中英

GCM Android-收到通知后不斷聽到聲音並振動

[英]GCM Android - continuously hear sound and vibrate when received notification

我將通知從GCM服務器推送到所有客戶端。 不斷聽到聲音並振動,直到我拉下通知欄。

這是我的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM