【问题标题】:Repeating notification sound重复提示音
【发布时间】:2014-10-20 08:37:21
【问题描述】:

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

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

【问题讨论】:

    标签: java android push-notification


    【解决方案1】:

    像这样:

            Notification note = mBuilder.build();
            //here
            note.flags = Notification.FLAG_INSISTENT;
    
            mNotificationManager.notify(1, note);
    

    【讨论】:

    • 欢迎来到 StackOverflow!简洁是可以接受的,但更全面的解释会更好。
    【解决方案2】:

    int FLAG_INSISTENT : 位被按位或位到标志字段中,如果设置,音频将重复直到通知被取消或通知窗口打开。

    关注android developer

    【讨论】:

    • FLAG_INSISTENT 在上面的 Oreo 上不起作用。您能提出一些建议吗?
    • 通常会有帮助。但是如果有一个活动的 GSM 呼叫,我的通知声音会被一个哔哔声取代。问题是即使设置了 FLAG_INSISTENT,此哔声也不会重复。任何想法如何让它重复?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多