2012-01-12 9 views
0

私は、電話機がAC2Mからプッシュ通知を受け取ったとき、通知バーに通知が表示されている必要があり、ユーザーが通知を押すと、自分のアプリを起動して、その通知を説明する特定のアクティビティを表示する必要があります私のアプリの通常の拳の活動。BroadcastReceiverは自分のアプリを起動できますか?

これは可能ですか?誰かが私にどのように説明できますか?

受信者を聴くには、アプリを起動する必要がありますか?私のアプリを起動できないのですか?

ありがとう

答えて

1

C2DMから、これが可能です。 C2DMReceiver.javaクラスの使用では

このコード:

@Override 
protected void onMessage(Context context, Intent intent) { 

    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     int icon = R.drawable.icon;  // icon from resources 
     CharSequence tickerText = "MyApp Notification";    // ticker-text 
     long when = System.currentTimeMillis();   // notification time 
     Context context21 = getApplicationContext();  // application Context 
     CharSequence contentTitle = "MyApp Notification Title"; // expanded message title 
     CharSequence contentText = (CharSequence) extras.get("message");  // expanded message text 
    Intent notificationIntent = new Intent(this, YourActivityName.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     Notification notification = new Notification(icon, tickerText, when); 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notification.defaults |= Notification.DEFAULT_LIGHTS; 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent); 
     mNotificationManager.notify(Constants.NOTIFICATION_ID, notification); 

    } 
} 

は(他の必要な必要な権限と一緒に)あなたのプロジェクトのAndroidManifest.xmlファイルに次のように宣言していることを確認して、あなたのアプリが聞くことを始めたようにするに:

<service android:name=".C2DMReceiver" /> 

<!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it 


     <receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> --> 
      <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 
       android:permission="com.google.android.c2dm.permission.SEND"> 
       <!-- Receive the actual message --> 
       <intent-filter> 
        <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
        <category android:name="com.your.packagename" /> 
       </intent-filter> 
       <!-- Receive the registration id --> 
       <intent-filter> 
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
        <category android:name="com.your.packagename" /> 
       </intent-filter> 
      </receiver> 
+0

いいえ、あなたは私の質問を理解できませんでした。私はアプリが起動していない必要があり、ブロードキャスト受信者がそれを開始しなければならないと伝えました。どうぞ、もう一度お読みになり、私にお答えください – NullPointerException

+0

通知のクリックやその他何が起こるべきかを表示したいですか?説明してください。 –

+0

私のアプリが起動していない状態で、電話機がAC2Mからプッシュ通知を受け取ったときに、通知バーに通知が表示されている必要があります。ユーザーが通知を押すと、アプリが起動され、私のアプリの通常の拳の活動ではなく、その通知を記述する。 – NullPointerException

関連する問題