2016-05-30 3 views
0

からの通知を行うためにだから私は近接アラートで苦労してきたし、最終的に私のコードは動作しますが、私はそれが私の代わりに、ログメッセージの通知を表示させる方法がわからない:どのようbroadcastreceiver

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

    } 
} 

は、まあ、私はこれを実装するためにしようと試みたが、私はそれは非推奨だと思う原因は動作しませんNotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);もproximityIntentReceiverクラスから

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

を何コンテキストを取得することはできません原因それは動作しません。 このクラスから通知を表示するにはどうすればよいですか?ありがとう

+0

onReceveメソッドのコンテキストが機能していない場合、コンストラクタ内のアプリケーションコンテキストで初期化しないのはなぜですか?また、onReceiveが実際に到達していることを確認してください。 –

+0

'getApplicationContext'の代わりに' onReceive'メソッドに渡された 'context'オブジェクトを使ってみることができます。ありがとう、働いています。 – jaibatrik

+0

私はあなたが私に代理人がいないので、あなたをアップヴォートすることはできません – Lucas

答えて

0

ブロードキャストレシーバでは、getApplicationContextの代わりにonReceiveメソッドに渡されたcontextオブジェクトを使用します。それは通知を実行する適切なコンテキストを与える必要があります。

関連する問題