2012-01-12 64 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 2012-01-12 15:06:04

+0

您想要通知点击时显示任何活动,否则会发生什么?请解释。 – 2012-01-12 15:11:49

+0

我希望通过我的应用程序NOT STARTED,当手机收到来自AC2M的推送通知时,必须在通知栏中显示通知,并且如果用户按下通知,我的应用程序必须启动并显示特定的活动描述该通知,而不是我的应用的正常拳头活动。 – NullPointerException 2012-01-12 15:32:47

相关问题