我想向我的 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>