我成功地从我的 GCM 服务器接收到我的 Android 应用程序的通知。
当我第一次选择我的通知时,我的通知会启动目标活动。但是,以下通知不会启动活动。
以下是我的 GCMIntentService 类:
public class GCMIntentService extends GCMBaseIntentService{
private String TAG = "RT-GCM";
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.e(TAG, "GCM-SERVICE: error");
}
@Override
protected void onMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
int nid = Integer.parseInt(intent.getStringExtra("nid"));
try {
// generateNotification(context, nid, intent.getStringExtra("message"));
generateNotification(context, intent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onRegistered(Context context, String arg1) {
// TODO Auto-generated method stub
Log.e(TAG, "GCM-SERVICE: Registered");
AccountManager accountManager = AccountManager.get(context);
accountManager.getAccounts();
Account account = getAccount(accountManager);
new GCMServerCall().register(arg1, "Tony", account.name);
}
@Override
protected void onUnregistered(Context arg0, String regID) {
// TODO Auto-generated method stub
Log.e(TAG, "GCM-SERVICE: UN-Registered");
new GCMServerCall().unregister(regID);
}
/**
* Issues a notification to inform the user that server has sent a message.
*
* I copied this code fragment from a GCM.zip example file downloaded from http://cl.ly/0C151616032t
*/
private static void generateNotification(Context context, Intent myintent) {
Integer nid = Integer.parseInt(myintent.getStringExtra("nid"));
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, myintent.getStringExtra("message") + "["+nid+"]", when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ArticleReader.class);
Log.e("TESTING ...", "Sending NID#" + nid);
notificationIntent.putExtra("NID", nid);
notificationIntent.putExtra("nid", nid);
notificationIntent.putExtra("test", "working");
// 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, myintent.getStringExtra("message") + "["+nid+"]", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(0, notification);
}
/****
* retrieving Google account(s) registered on the target device
* @param accountManager
* @return
*/
private Account getAccount(AccountManager accountManager){
Account[] accounts = accountManager.getAccountsByType("com.google");
Log.i(TAG, "We have " + accounts.length + " accounts!");
return accounts[0];
}
}
更新:
我发现了一些新东西。在第一次也是唯一一次通过选择通知成功启动 ArticleReader.class 之后,仅当我将应用程序从 ArticleReader 中导航出来时,选择以下通知才有效。换句话说,如果 ArticleReader 的实例是活动视图,则任何尝试启动相同活动的通知都将不起作用。
任何人都可以告诉我如何通过通知重复启动 ActivityReader 类,即使 ActivityReader 活动是当前视图,好吗?