我创建了gcm推送通知,当服务器发送通知然后我点击通知状态栏它会启动新活动,但不会显示消息。 当我打开消息接收器活动并从服务器发送消息时,屏幕上会显示消息。
这是我的GCMIntentService.java文件
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
// long when = System.currentTimeMillis();
// Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message);
Intent notificationIntent = new Intent(context, ServerActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ServerActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(notificationIntent);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notification.setContentIntent(intent);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(number, notification.build());
}
}
这是我的ServerActivity.java文件
public class ServerActivity extends ActionBarActivity {
// label to display gcm messages
TextView lblMessage;
Controller aController;
static final int READ_BLOCK_SIZE = 100;
// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;
public static String prn;
public static String pass;
public static String notice;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serveractivity);
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
//Get Global Controller Class object (see application tag in AndroidManifest.xml)
aController = (Controller) getApplicationContext();
// Check if Internet present
if (!aController.isConnectingToInternet()) {
// Internet Connection is not present
aController.showAlertDialog(ServerActivity.this,
"Internet Connection Error",
"Please connect to Internet connection", false);
// stop executing code by return
return;
}
// Getting name, email from intent
Intent i = getIntent();
prn = i.getStringExtra("prn");
pass = i.getStringExtra("pass");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest permissions was properly set
GCMRegistrar.checkManifest(this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
Linkify.addLinks(lblMessage, Linkify.ALL);
lblMessage.setMovementMethod(LinkMovementMethod.getInstance());
// Register custom Broadcast receiver to show messages on activity
registerReceiver(mHandleMessageReceiver, new IntentFilter(
Config.DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already presents
if (regId.equals("")) {
// Register with GCM
GCMRegistrar.register(this, Config.GOOGLE_SENDER_ID);
} else {
// Device is already registered on GCM Server
if (GCMRegistrar.isRegisteredOnServer(this)) {
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
aController.register(context, prn, pass, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
// execute AsyncTask
mRegisterTask.execute(null, null, null);
}
}
}
// Create a broadcast receiver to get message and show on screen
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
aController.acquireWakeLock(getApplicationContext());
// Display message on the screen
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(),
"Notice: " + newMessage,
Toast.LENGTH_LONG).show();
// Releasing wake lock
aController.releaseWakeLock();
}
};
@Override
protected void onDestroy() {
// Cancel AsyncTask
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
// Unregister Broadcast Receiver
unregisterReceiver(mHandleMessageReceiver);
//Clear internal resources.
GCMRegistrar.onDestroy(this);
} catch (Exception e) {
}
super.onDestroy();
}
}
这是我的serveractivity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f661"
android:orientation="vertical" >
<TextView
android:id="@+id/lblMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
谢谢。