本文介绍了GCM Android - 收到通知时不断听到声音和震动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通知从GCM服务器推送到所有客户端。
连续听到声音并震动,直到我拉下通知栏。

I push notification from GCM Server to all clients. Continuously hear sound and vibrate until I pull down the notification bar.

以下是我的代码:

Here's my code:

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

我只想让它正常(声音和振动只有一次)。

I just wanna let it normally (Sound and Vibrate only 1 time).

如何解决它?

谢谢。

推荐答案

使用以下代码

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

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

这篇关于GCM Android - 收到通知时不断听到声音和震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:41