1.定义IntentService,接收后台notification消息,从而触发。
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("");
}
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra("message");
String url = intent.getStringExtra("url");
generateNotification(getString(R.string.app_name), message, url);
}
private void generateNotification(String title, String content, String url) {
long when = System.currentTimeMillis();
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_TITLE, title);
notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_CONTENT, content);
notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_WEBURL, url);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setWhen(when);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) when, builder.build());
}
}
当后台接收到消息时,会触发这个service,onMessage会调用,然后产生一个notification,用户点击后跳转到MainActivity。
2.定义MainActivity接收notification的数据,然后以对话框的形式弹出。
public class MainActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//handle notification when app at background
Bundle bundle = getIntent().getExtras();
if (null != bundle) {
final String title = bundle.getString(ConstantUtil.BUNDLE_KEY_TITLE);
final String content = bundle.getString(ConstantUtil.BUNDLE_KEY_CONTENT);
final String url = bundle.getString(ConstantUtil.BUNDLE_KEY_WEBURL);
if (!StringUtil.isEmpty(content))
showNotificationDialog(title, content, url);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//handld notification when app at foreground
Bundle bundle = intent.getExtras();
if (null != bundle) {
final String title = bundle.getString(ConstantUtil.BUNDLE_KEY_TITLE);
final String content = bundle.getString(ConstantUtil.BUNDLE_KEY_CONTENT);
final String url = bundle.getString(ConstantUtil.BUNDLE_KEY_WEBURL);
if (!StringUtil.isEmpty(content))
showNotificationDialog(title, content, url);
}
}
}
当应用运行在后台的时候,notification到达时,会调用onCreate(),当应用在前端显示时,notification到达时,会调用onNewIntent(),所以需要单独处理每一种情况,此外必须注意的是onNewIntent()方法,代码:
Bundle bundle = intent.getExtras();
// Bundle bundle = getIntent().getExtras();
不能使用getIntent()的方法去取notification传过来的数据,getIntent()取到的intent是之前的intent,所以应该使用onNewIntent(Intent intent)的intent去获取数据。
3.AndroidManifest文件里定义:
<activity android:name=".MainActivity"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
android:screenOrientation="portrait"/>
<service android:name=".GCMIntentService"/>
大致流程就是如此了。