2016-11-30 35 views
1

我在Youtube上發佈了一些教程的通知。我認爲一切正常,因爲我看到了通知。問題是我在第二天等待通知。它不起作用。它沒有出現。每日本地通知只出現一次 - Android

這裏是小時等代碼:

button1 = (Button)dialog.findViewById(R.id.btnRegister); 

    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Calendar calendar = Calendar.getInstance(); 

      calendar.set(Calendar.HOUR_OF_DAY, 20); 
      calendar.set(Calendar.MINUTE, 30); 
      calendar.set(Calendar.SECOND, 30); 

      Intent intent = new Intent(getApplicationContext(), Notification_receiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

哦,它開始wiith的onclick按鈕,工作在我的自定義對話框,只與第一個應用程序啓動apear。請幫助我們!

編輯

NotificationReceiver的java:

package com.justfashion; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationBuilderWithBuilderAccessor; 
import android.support.v4.app.NotificationCompat; 

/** 
* Created by otsma on 24.11.2016. 
*/ 
public class Notification_receiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent repeating_intent = new Intent(context,ActivitySplashScreen.class); 
     repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(android.R.drawable.arrow_up_float) 
       .setContentTitle("Hey You!") 
       .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound)) 
       .setContentText("Collect today's diamonds!") 
       .setAutoCancel(true); 

     notificationManager.notify(100,builder.build()); 


    } 





} 

回答

0

如果您的應用程序在此期間重新啓動警報獲取cancelled.so在另一天沒有通知。所以使用廣播接收機解決此問題。並請不要使用服務只顯示notification.your方法是對的。

enter code here 

    public class BootReceiver extends BroadcastReceiver { 



    @Override 
    public void onReceive(Context context, Intent intent) { 



     Log.e("BootReceiver","BootReceiver"); 

     if  (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      // Set the alarm here. 



    } 

    } 



} 

    <receiver android:name=".alarmManager.BootReceiver" 
      android:enabled="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
      </intent-filter> 
     </receiver> 

使用許可:

+0

加入我的接收器的代碼 –

+0

添加額外的接收器:BootReceiver聽引導和時間表也從那裏發出警報。 – ak0692

+0

我添加了代碼! – ak0692

0

與報警可能的問題已經設置 試試這個

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
    AlarmManager.INTERVAL_DAY, alarmIntent); 

如前所述herehere

0

在初始屏幕或主屏幕中實現此操作 此方法設置一天中特定時間的鬧鐘。

public void startWeatherAlert() { 
     Calendar updateTime = Calendar.getInstance(); 
     updateTime.setTimeZone(TimeZone.getTimeZone("IST")); 
     updateTime.set(Calendar.HOUR_OF_DAY, 8);//set hour of day when you want to trigger 
     updateTime.set(Calendar.MINUTE, 15);//set minute of day 

     Intent intent = new Intent(this, YourReceiverNameHere.class);//Broadcast receiver here 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);//INTERVAL_DAY means that it will trigger only once in a day. 
    } 

現在設置的廣播接收器

public class YourReceiverNameHere extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      //your logic here 
      //this can be used to invoke service, launch activity or to generate notification 
     } 
    } 

最後註冊該廣播接收機menifest

<service android:name=".YourReceiverNameHere" /> 
+0

不工作夥伴 –