重复通知声音

5

是否可以(如果可以的话,如何)使推送通知声音重复,直到其被阅读?我正在创建应用程序,以通知用户应用程序中的新事件,但用户需要尽快阅读通知。当用户“阅读”通知时,它应该停止响铃。以下是代码:

public class GCMIntentService extends IntentService {
String mes;
HelperGlobals glob;

public GCMIntentService() {
    super("GcmIntentService");
}


@SuppressLint("SimpleDateFormat")
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    glob = (HelperGlobals) getApplicationContext();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    // .... Doing work here

    GcmBroadcastReceiver.completeWakefulIntent(intent);

}


public void createPush(String title, String msg, Intent intent) {

    Uri soundUri = Uri.parse("android.resource://example.project.com/" + R.raw.notification);

    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(context, DoNothing.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification n  = new Notification.Builder(this)
            .setContentTitle(title)
            .setContentText(msg)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true).build(); 
    n.defaults |= Notification.DEFAULT_VIBRATE;
    //n.defaults |= Notification.DEFAULT_SOUND; 
    n.sound = soundUri;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, n); 
}

}

广播接收器:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("BukuLog", "Receiver");
    // Explicitly specify that GcmMessageHandler will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GCMIntentService.class.getName());

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));

    setResultCode(Activity.RESULT_OK);
}
}
2个回答

22

像这样:

        Notification note = mBuilder.build();
        //here
        note.flags = Notification.FLAG_INSISTENT;

        mNotificationManager.notify(1, note);

3
欢迎来到StackOverflow!简洁明了的表达是可以接受的,但更详细的解释会更好。 - naXa stands with Ukraine

16

int FLAG_INSISTENT : 如果设置了这个标志位,音频将会一直重复播放,直到通知被取消或通知窗口被打开。

参考Android Developer


2
FLAG_INSISTENT在Oreo以上版本无法正常工作,您能否提供一些建议? - Pooja Shukla
一般情况下这很有用。但是如果有一个活动的 GSM 通话,我的通知声音将被替换为单个哔哔声。问题在于,即使设置了 FLAG_INSISTENT,这个哔哔声也不会重复。有什么想法可以让它也重复呢? - Dmitry K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接