2012-12-01 149 views
1

我想发送通知到我的android设备。 问题是在正确发送和接收的第一个通知之后,我必须关闭(kill)并打开应用程序以接收另一个通知。我不知道我必须改变。非常感谢您GCM-Android更多通知

我编程,这样应用程序(如谷歌为例):

public class FirstActivity extends Activity 
{ 
    String TAG = "FirstActivity"; 
    String SENDER_ID = "123456789101"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_first); 
     GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     final String regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      GCMRegistrar.register(this, SENDER_ID); 
     } else { 
      Log.v(TAG, "Already registered"); 
     } 
    } 
} 

和:

public class GCMIntentService extends GCMBaseIntentService 
{ 
    String TAG = "GCMIntentService"; 
    public GCMIntentService() 
    { 
     // TODO Auto-generated constructor stub 
    } 
    @Override 
    protected void onError(Context arg0, String arg1) 
    { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onError"); 
    } 

    @Override 
    protected void onMessage(Context arg0, Intent arg1) 
    { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onMessage"); 
     generateNotification(arg0, arg1.getStringExtra("message")); 

    } 
    private static void generateNotification(Context context, String message) { 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, 
       message, when); 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, 
       FirstActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
    } 


    @Override 
    protected void onRegistered(Context arg0, String arg1) 
    { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onRegistered"); 
    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) 
    { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onUnregistered"); 
    } 

} 

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="at.demo.gcm" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

    <permission 
     android:name="at.demo.gcm.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="at.demo.gcm.permission.C2D_MESSAGE" /> 

    <!-- App receives GCM messages. --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <!-- GCM connects to Google Services. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <!-- Keeps the processor from sleeping when a message is received. --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="at.demo.gcm.FirstActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="at.demo.gcm" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".GCMIntentService" /> 
    </application> 

</manifest> 

回答

2

GCMIntentService类必须的构造有SENDER_ID这是你的情况,如:

private static final String SENDER_ID = "123456789101"; 

public GCMIntentService() 
    { 
     super(SENDER_ID); 
    } 

希望这会帮助你,因为我在simellar应用程序想你的代码,我有工作,我收到通知,只要我从网络服务器发送他们,而不需要杀死应用程序

0

GCM不是问题在这里。问题在于你如何进行generateNotification(调试应该显示)。您对所有通知0都使用一个ID,因此您无法通过此方式更新通知。每次更改ID。