目前我正在研究 GCM(谷歌云消息),它允许用户将消息推送到用户设备。我想达到以下要求:
如果用户已经进入app,则忽略
如果用户没有进入应用,点击通知进入应用
我的应用程序的工作流程是:
- WelcomePage(下载 json 并从中创建数据集)=> MainPage(根据数据集显示)
处理通知的代码
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String notifyMsg = "";
JSONTokener tokener = new JSONTokener(msg);
if (tokener != null) {
try {
notifyMsg = new JSONObject(tokener).getString("msg");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent myintent = new Intent(this, WelcomePageActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notifyMsg))
.setContentText(notifyMsg)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
问题是如果我使用 WelcomePageActivity 类,如果我在主页面,它会创建一个新 Activity ,我如何调整代码以满足我的要求?
谢谢
最佳答案
为了
1.如果用户已经输入过app,则忽略:
在 onReceive()
中,检查您的应用程序是否正在运行,不要通知。
它可以用类似的东西来检查:
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
break;
}
}
- 如果用户没有进入应用,点击通知进入应用
从上面的代码中,您知道是否要恢复应用程序或启动 - 调用 Splash Screen 或在您的情况下调用 WelcomeActivity。
关于您的应用程序的工作流程,我建议您检查是否需要每次都下载数据。可以保存它或仅在需要时更新/下载,其余流程按原样工作。
关于android - 在android中点击通知进入我的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058422/