2015-07-22 76 views
0

我想在特定時間和日常活動中顯示通知(即在8點鐘的早上)。我正在使用廣播接收器,警報管理器來顯示通知。 但問題是,我沒有收到移動設備上顯示的通知。 我也在清單文件中添加了喚醒鎖權限。請幫助我 謝謝在特定時間顯示通知

MainActivity.java

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 8); 
    calendar.set(Calendar.MINUTE, 00); 
    calendar.set(Calendar.SECOND, 0); 

    Intent notificationmassage = new Intent(getApplicationContext(),NotificationClass.class); 

    //This is alarm manager 
    PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, pi); 

    Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show(); 

} 

NotificationClass.java

public class NotificationClass extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(context, MainActivity.class), 0); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
               .setSmallIcon(R.drawable.ic_launcher) 
               .setContentTitle("Text1") 
               .setContentText("Text2 "); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager ; 
     mNotificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1,mBuilder.build()); 

    } 
} 

清單:

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

<uses-permission android:name="android.permission.WAKE_LOCK"/> 


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver android:name=".NotificationClass"> 
     </receiver> 
</application> 

UPDAT E: - 現在問題出現在我打開應用程序通知時出現。對於例如:警報設置爲8.AM,每當我在上午8點之後打開應用程序,就會發出通知。即使當前時間>上午8點。如何解決這個問題?

回答

1

在你的活動替換以下

PendingIntent pi = PendingIntent.getService(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 

PendingIntent pi = PendingIntent.getBroadcast(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

好的。當我打開應用程序時會顯示通知。當我關閉應用程序時,它不會顯示它。我認爲它不是作爲一個進程在後臺運行 – Karan

+0

如何將它作爲後臺進程運行..這樣即使我的應用程序關閉,它也會在特定時間顯示通知? – Karan

+1

您正在使用AlarmManager.RTC_WAKEUP,因此,一旦您調用am.setRepeating(...),它應該在所需的時間喚醒設備。自動執行接收器並顯示通知。 – DaniZ

0
 public void getNotification(Context context,String Message){  
      int icon = R.drawable.appicon; 
      int when =(int) System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) 
        context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(icon, Message, when); 

      String title = context.getString(R.string.app_name); 

      Intent notificationIntent = new Intent(context, MainActivity.class); 

      // set intent so it does not start a new activity 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, 0); 
      notification.setLatestEventInfo(context, title,Message, intent); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 

      // Play default notification sound 
      notification.defaults |= Notification.DEFAULT_SOUND; 

      // Vibrate if vibrate is enabled 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notificationManager.notify(0, notification); 

    } 
+0

我想每天或常規像@ 8'o時鐘在對特定時間顯示通知早上。怎麼做? – Karan

+0

如何將它作爲後臺進程運行..以便即使我的應用程序關閉,它也會在特定時間顯示通知? – Karan