2017-07-23 3 views
0

Ich versuche zu lernen, Anwendungen für Android-Geräte zu entwickeln. Seit einiger Zeit entwickle ich eine Anwendung, in der es eine Funktion zum Senden von Benachrichtigungen (Erinnerungen) zu verschiedenen Tageszeiten gibt. Aber wenn das Telefon gesperrt ist und die Zeit zum Senden der Benachrichtigung mehr als 10 Minuten beträgt, kommen sie nicht. Versuchte BroadcasrReceiver:Keine Benachrichtigungen anzeigen Android

public class NotificationReceiver extends WakefulBroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    addNotif(context, "Times UP Receiver", "5 second Receiver", "Alert Receiver"); 
} 

private void addNotif(Context context, String msg, String msgText, String msgAlert) { 
    Notification.Builder builder = new Notification.Builder(context); 
    Intent intent = new Intent(context, MainActivity.class); 
    PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent) 
      .setSmallIcon(R.drawable.ic_android_black_24dp) 
      .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp)) 
      .setTicker(msgAlert) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle(msg) 
      .setContentText(msgText); 

    Notification notification = builder.build(); 
    notification.defaults = Notification.DEFAULT_ALL; 

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    nm.notify(101, notification); 
} 
} 

Verwendung Alarmmanager:

 Calendar calNotif = Calendar.getInstance(); 
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 102, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE); 
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (15 * 60 * 1000), pendingIntent); 

Ich versuchte auch Service zu nutzen:

public class NotificationService extends Service { 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Calendar calNotif = Calendar.getInstance(); 
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 103, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE); 
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (16 * 60 * 1000), pendingIntent); 
    return START_STICKY; 
} 
} 

Manifest:

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

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

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver android:name=".NotificationReceiver" /> 
    <service android:name=".NotificationService" /> 

Aber nichts funktioniert. Mein Kopf tut weh, ich kann immer noch nicht verstehen, warum Benachrichtigungen nicht kommen. Sag mir bitte, was ist das Problem.

Antwort

0

Verwenden Sie BaseContext nicht und gehen Sie zur Testbenachrichtigung. Warum sollten Sie dann 15 Minuten für Benachrichtigungen warten? Minimiere deine Zeit und überprüfe nach den Arbeiten deine Zeit.

In YourActivity

Calendar calNotif = Calendar.getInstance(); 
Intent alertIntent = new Intent(this, NotificationReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 102, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) 
notifAlarm.setExact(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent); 
else 
notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent); 

In Ihrem Notification-Code

builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent) 
     .setSmallIcon(R.drawable.ic_android_black_24dp) 
     .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp)) 
     .setTicker(msgAlert) 
     .setWhen(System.currentTimeMillis()) 
     .setContentTitle(msg) 
     .setContentText(msgText); 

Entfernen setLargeIcon beacause es Bitmap verwendet und mehr Speicher nimmt. Nachdem es funktioniert hat, können Sie den Service nutzen.

Verwandte Themen