简体   繁体   中英

Repeating notification sound

Is it possible (and if yes how) to make push notification sound repeat until it's read? I am creating app that notifies user about new event in app, but user needs to read notification as soon as possible. When user "reads" notification it should stop ringing. Here's code:

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

}

And BroadcastReceiver:

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

like this:

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

        mNotificationManager.notify(1, note);

int FLAG_INSISTENT : Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.

follow android developer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM