2016-05-30 8 views
0

Also habe ich mit Proximity-Benachrichtigungen zu kämpfen und schließlich mein Code funktioniert, aber ich weiß nicht, wie es mir eine Benachrichtigung statt einer Protokollmeldung zu machen:Wie eine Benachrichtigung von broadcastreceiver machen

public class ProximityIntentReceiver extends BroadcastReceiver { 

private static final int NOTIFICATION_ID = 1000; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d(getClass().getSimpleName(), "in receiver"); 

    String key = LocationManager.KEY_PROXIMITY_ENTERING; 

    Boolean entering = intent.getBooleanExtra(key, false); 

    if (entering) { 
     Log.d(getClass().getSimpleName(), "entering receiverrrrrrrrrrrrrrrrrr"); 
    } else { 
     Log.d(getClass().getSimpleName(), "exiting"); 
    } 

    } 
} 

Nun, ich habe versucht, dies zu implementieren, aber es wird nicht funktionieren, weil ich

Intent notificationIntent = new Intent(getApplicationContext(),NotificationView.class); 

    /** Adding content to the notificationIntent, which will be displayed on 
    * viewing the notification 
    */ 
    notificationIntent.putExtra("content", notificationContent); 

    /** This is needed to make this intent different from its previous intents */ 
    notificationIntent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis())); 

    /** Creating different tasks for each notification. See the flag Intent.FLAG_ACTIVITY_NEW_TASK */ 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    /** Getting the System service NotificationManager */ 
    NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

    /** Configuring notification builder to create a notification */ 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()) 
      .setWhen(System.currentTimeMillis()) 
      .setContentText(notificationContent) 
      .setContentTitle(notificationTitle) 
      .setSmallIcon(R.drawable.ic_menu_notification) 
      .setAutoCancel(true) 
      .setTicker(tickerMessage) 
      .setContentIntent(pendingIntent) 
      .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

    /** Creating a notification from the notification builder */ 
    Notification notification = notificationBuilder.build(); 

    /** Sending the notification to system. 
    * The first argument ensures that each notification is having a unique id 
    * If two notifications share same notification id, then the last notification replaces the first notification 
    * */ 
    nManager.notify((int)System.currentTimeMillis(), notification); 

auch NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); wird nicht funktionieren, weil ich veraltete es erraten keinen Kontext von proximityIntentReceiver Klasse erhalten kann. Wie kann ich eine Benachrichtigung von dieser Klasse anzeigen? Danke

+0

Wenn der Kontext in onReceve Methode nicht funktioniert, dann warum es nicht mit dem Anwendungskontext im Konstruktor initialisieren? Stellen Sie außerdem sicher, dass onReceive tatsächlich erreicht wird. –

+0

Anstelle von 'getApplicationContext' können Sie versuchen, das' context' Objekt zu verwenden, das an die 'onReceive' Methode übergeben wird. – jaibatrik

+0

danke, funktioniert. Ich kann Sie nicht upvote, weil ich keine Wiederholung habe, tut mir leid – Lucas

Antwort

0

Verwenden Sie in Ihrem BroadcastReceiver das Objekt context, das an die onReceive-Methode übergeben wurde, anstatt an getApplicationContext. Dies sollte der Benachrichtigung den richtigen Kontext zum Ausführen geben.