2014-10-20 125 views
1

是否有可能(如果是如何)使推送通知聲音重複直到它被讀取?我正在創建應用程序,通知用戶有關應用程序中的新事件,但用戶需要儘快閱讀通知。當用戶「讀取」通知時,它應該停止響鈴。這裏是代碼:重複通知聲音

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

回答

4

INT FLAG_INSISTENT:位進行按位或爲標誌字段,如果設置,音頻將重複進行,直到通知被取消或通知窗口打開。

遵循android developer

+0

謝謝!有效。 – 2014-10-20 09:45:25

2

這樣的:

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

     mNotificationManager.notify(1, note); 
+0

歡迎來到StackOverflow!簡潔是可以接受的,但更全面的解釋更好。 – naXa 2015-11-19 12:08:13