BroadcastReceiver может запустить мое собственное приложение? - PullRequest
0 голосов
/ 12 января 2012

Я хочу, чтобы, когда телефон получил push-уведомление от AC2M, в панели уведомлений должно отображаться уведомление, и если пользователь нажимает на уведомление, мое приложение должно быть запущено и отображать определенную активность, описывающую это уведомление, не нормальная кулачная активность моего приложения.

Можно этого добиться? может кто-нибудь объяснить мне, как?

Мое приложение должно быть запущено для прослушивания приемника? или мое приложение не запускается?

спасибо

1 Ответ

1 голос
/ 12 января 2012

Из 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...