Skip to content

Commit fa42cae

Browse files
committed
发现飞鸽也会创建一个MyApp类,导致收到的push被处理了两次
1 parent 6335be6 commit fa42cae

10 files changed

Lines changed: 182 additions & 112 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="net.coding.program"
44
android:versionCode="26"
5-
android:versionName="2.3">
5+
android:versionName="2.3" >
66

77
<uses-sdk
88
android:minSdkVersion="15"
@@ -30,18 +30,19 @@
3030
<uses-permission android:name="android.permission.READ_LOGS" />
3131
<uses-permission android:name="android.permission.VIBRATE" />
3232

33+
3334
<application
3435
android:name=".MyApp"
3536
android:allowBackup="true"
3637
android:icon="@drawable/ic_launcher"
3738
android:label="@string/app_name"
38-
android:theme="@style/ActionbarNoIcon">
39+
android:theme="@style/ActionbarNoIcon" >
3940

4041
<!-- 【必须】 信鸽receiver广播接收 -->
4142
<receiver
4243
android:name="com.tencent.android.tpush.XGPushReceiver"
43-
android:process=":xg_service_v2">
44-
<intent-filter android:priority="0x7fffffff">
44+
android:process=":xg_service_v2" >
45+
<intent-filter android:priority="0x7fffffff" >
4546

4647
<!-- 【必须】 信鸽SDK的内部广播 -->
4748
<action android:name="com.tencent.android.tpush.action.SDK" />
@@ -61,7 +62,7 @@
6162
<!-- 【注意】 如果被打开的activity是启动模式为SingleTop,SingleTask或SingleInstance,请根据通知的异常自查列表第8点处理 -->
6263
<activity
6364
android:name="com.tencent.android.tpush.XGPushActivity"
64-
android:exported="true">
65+
android:exported="true" >
6566
<intent-filter>
6667
<action android:name="android.intent.action" />
6768
</intent-filter>
@@ -74,7 +75,7 @@
7475
android:persistent="true"
7576
android:process=":xg_service_v2" />
7677

77-
<receiver android:name=".common.PushReceiver">
78+
<receiver android:name=".common.PushReceiver" >
7879
<intent-filter>
7980

8081
<!-- 接收消息透传 -->
@@ -87,7 +88,7 @@
8788
<!-- 【必须】 通知service,此选项有助于提高抵达率 -->
8889
<service
8990
android:name="com.tencent.android.tpush.rpc.XGRemoteService"
90-
android:exported="true">
91+
android:exported="true" >
9192
<intent-filter>
9293
<action android:name="net.coding.program.PUSH_ACTION" />
9394
</intent-filter>
@@ -105,7 +106,7 @@
105106
<activity
106107
android:name=".MainActivity_"
107108
android:label="@string/app_name"
108-
android:launchMode="singleInstance"
109+
android:launchMode="singleTask"
109110
android:screenOrientation="portrait"
110111
android:theme="@style/ActionbarSpannerTheme" />
111112
<activity
@@ -119,7 +120,7 @@
119120
android:name=".project.detail.ProjectActivity_"
120121
android:icon="@drawable/ic_lancher"
121122
android:label="@string/title_activity_project"
122-
android:screenOrientation="portrait">
123+
android:screenOrientation="portrait" >
123124
<intent-filter>
124125
<data
125126
android:host="ProjectActivity_"
@@ -135,7 +136,7 @@
135136
android:name=".EntranceActivity_"
136137
android:icon="@drawable/ic_launcher"
137138
android:screenOrientation="portrait"
138-
android:theme="@style/AppThemeNoActionbar">
139+
android:theme="@style/AppThemeNoActionbar" >
139140
<intent-filter>
140141
<action android:name="android.intent.action.MAIN" />
141142

@@ -220,7 +221,7 @@
220221
android:icon="@drawable/ic_lancher"
221222
android:label="@string/title_activity_topic_comment"
222223
android:screenOrientation="portrait"
223-
android:windowSoftInputMode="stateHidden|adjustResize">
224+
android:windowSoftInputMode="stateHidden|adjustResize" >
224225
<intent-filter>
225226
<data
226227
android:host="TopicListDetailActivity_"
@@ -243,7 +244,7 @@
243244
android:icon="@drawable/ic_launcher"
244245
android:label="@string/title_activity_make_maopao"
245246
android:screenOrientation="portrait"
246-
android:windowSoftInputMode="stateHidden|adjustResize">
247+
android:windowSoftInputMode="stateHidden|adjustResize" >
247248
<intent-filter>
248249
<action android:name="android.intent.action.SEND" />
249250

@@ -280,7 +281,7 @@
280281
android:name=".user.UserDetailActivity_"
281282
android:icon="@drawable/ic_lancher"
282283
android:label="@string/title_activity_user_detail"
283-
android:screenOrientation="portrait">
284+
android:screenOrientation="portrait" >
284285
<intent-filter>
285286
<data
286287
android:host="UserDetailActivity_"
@@ -318,7 +319,7 @@
318319
android:icon="@drawable/ic_lancher"
319320
android:label="@string/title_activity_maopao_detail"
320321
android:screenOrientation="portrait"
321-
android:windowSoftInputMode="stateHidden|adjustUnspecified"></activity>
322+
android:windowSoftInputMode="stateHidden|adjustUnspecified" />
322323
<activity
323324
android:name=".user.UserMaopaoActivity"
324325
android:icon="@drawable/ic_lancher"
@@ -377,8 +378,8 @@
377378
android:name=".WebActivity_"
378379
android:icon="@drawable/ic_lancher"
379380
android:label="@string/title_activity_account_setting"
380-
android:theme="@style/AppThemeNoActionbar"
381-
android:screenOrientation="portrait" />
381+
android:screenOrientation="portrait"
382+
android:theme="@style/AppThemeNoActionbar" />
382383
<activity
383384
android:name=".project.SearchProjectActivity_"
384385
android:icon="@drawable/ic_lancher"
@@ -413,16 +414,31 @@
413414
android:icon="@drawable/ic_lancher"
414415
android:label="@string/title_activity_update_tip"
415416
android:screenOrientation="portrait"
416-
android:theme="@android:style/Theme.Holo.Light.Dialog"></activity>
417+
android:theme="@android:style/Theme.Holo.Light.Dialog" />
417418
<activity
418419
android:name=".FileUrlActivity_"
419420
android:icon="@drawable/ic_lancher"
420421
android:label="@string/title_activity_file_url"
421-
android:screenOrientation="portrait"></activity>
422+
android:screenOrientation="portrait" />
422423
<activity
423424
android:name=".project.ProjectHomeActivity_"
424425
android:label="@string/title_activity_project_home"
425-
android:screenOrientation="portrait"></activity>
426+
android:screenOrientation="portrait" />
427+
<activity
428+
android:name=".TestActivity"
429+
android:label="@string/title_activity_test" >
430+
</activity>
431+
432+
<receiver
433+
android:name=".MyPushReceiver"
434+
android:enabled="true"
435+
android:exported="false" >
436+
437+
<intent-filter>
438+
<action android:name="PushClickBroadcast" />
439+
</intent-filter>
440+
441+
</receiver>
426442
</application>
427443

428444
</manifest>

app/src/main/java/net/coding/program/MainActivity.java

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.coding.program;
22

3-
import android.support.v7.app.ActionBar;
43
import android.content.BroadcastReceiver;
54
import android.content.Context;
65
import android.content.Intent;
@@ -10,6 +9,7 @@
109
import android.support.v4.app.FragmentManager;
1110
import android.support.v4.app.FragmentTransaction;
1211
import android.support.v4.widget.DrawerLayout;
12+
import android.support.v7.app.ActionBar;
1313
import android.util.Log;
1414
import android.view.LayoutInflater;
1515
import android.view.Menu;
@@ -40,21 +40,20 @@
4040
import org.androidannotations.annotations.ViewById;
4141
import org.androidannotations.annotations.res.StringArrayRes;
4242

43-
import java.util.HashSet;
4443
import java.util.List;
4544

4645
@EActivity(R.layout.activity_main)
4746
public class MainActivity extends BaseActivity
4847
implements NavigationDrawerFragment_.NavigationDrawerCallbacks {
4948

49+
public static final String TAG = "MainActivity";
50+
5051
NavigationDrawerFragment_ mNavigationDrawerFragment;
5152
String mTitle;
5253

5354
@Extra
5455
String mPushUrl;
5556

56-
// HashSet<String> mPushOpened = new HashSet();
57-
5857
@StringArrayRes
5958
String drawer_title[];
6059

@@ -69,20 +68,6 @@ public class MainActivity extends BaseActivity
6968
boolean mFirstEnter = true;
7069
private View actionbarCustom;
7170

72-
static private boolean mJumpNewIntent = false;
73-
74-
// 防止在onNewIntent中重复打开
75-
public static void setJumpNewIntent() {
76-
mJumpNewIntent = true;
77-
}
78-
79-
public static boolean getJumpNewIntent() {
80-
boolean old = mJumpNewIntent;
81-
mJumpNewIntent = false;
82-
return old;
83-
}
84-
85-
8671
@Override
8772
protected void onCreate(Bundle savedInstanceState) {
8873
super.onCreate(savedInstanceState);
@@ -106,30 +91,27 @@ protected void onCreate(Bundle savedInstanceState) {
10691
// mPushOpened = (HashSet<String>) savedInstanceState.getSerializable("mPushOpened");
10792
mTitle = savedInstanceState.getString("mTitle");
10893
}
109-
}
110-
111-
@Override
112-
protected void onNewIntent(Intent intent) {
113-
super.onNewIntent(intent);
114-
setIntent(intent);
11594

116-
// if (mPushUrl != null && !mPushOpened.contains(mPushUrl)) {
117-
// mPushOpened.add(mPushUrl);
118-
// }
95+
Log.d(TAG, "onCreate");
11996

120-
if (!getJumpNewIntent()) {
121-
if (mPushUrl != null) {
122-
URLSpanNoUnderline.openActivityByUri(this, mPushUrl, true);
123-
}
97+
if (mPushUrl != null) {
98+
URLSpanNoUnderline.openActivityByUri(this, mPushUrl, true);
12499
}
125100
}
126101

102+
127103
@Override
128104
protected void onDestroy() {
129105
unregisterReceiver(mUpdatePushReceiver);
130106

131107
super.onDestroy();
108+
}
109+
110+
@Override
111+
public void finish() {
132112
MyApp.setMainActivityState(false);
113+
114+
super.finish();
133115
}
134116

135117
// 信鸽文档推荐调用,防止在小米手机上收不到推送
@@ -146,7 +128,7 @@ private void updateNotifyService() {
146128
String globalKey = MyApp.sUserObject.global_key;
147129
XGPushManager.registerPush(this, globalKey);
148130
} else {
149-
XGPushManager.unregisterPush(this);
131+
XGPushManager.registerPush(this, "*");
150132
}
151133
}
152134

@@ -174,6 +156,7 @@ void init() {
174156
spinner.setAdapter(mSpinnerAdapter);
175157
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
176158
String[] strings = getResources().getStringArray(R.array.maopao_action_types);
159+
177160
@Override
178161
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
179162
Fragment fragment;

app/src/main/java/net/coding/program/MyApp.java

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package net.coding.program;
22

33
import android.app.Application;
4-
import android.app.NotificationManager;
5-
import android.content.BroadcastReceiver;
64
import android.content.Context;
7-
import android.content.Intent;
8-
import android.content.IntentFilter;
5+
import android.util.Log;
96

10-
import com.loopj.android.http.AsyncHttpClient;
11-
import com.loopj.android.http.JsonHttpResponseHandler;
127
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
138
import com.nostra13.universalimageloader.core.ImageLoader;
149
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
1510
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
1611

17-
import net.coding.program.common.Global;
18-
import net.coding.program.common.PushReceiver;
1912
import net.coding.program.common.Unread;
20-
import net.coding.program.common.htmltext.URLSpanNoUnderline;
21-
import net.coding.program.common.network.MyAsyncHttpClient;
2213
import net.coding.program.model.AccountInfo;
2314
import net.coding.program.model.UserObject;
2415
import net.coding.program.third.MyImageDownloader;
@@ -37,18 +28,21 @@ public class MyApp extends Application {
3728

3829
public static UserObject sUserObject;
3930
public static Unread sUnread;
31+
4032
public static boolean sMainCreate = false;
4133

4234
public static void setMainActivityState(boolean create) {
4335
sMainCreate = create;
4436
}
4537

38+
public static boolean getMainActivityState() {
39+
return sMainCreate;
40+
}
41+
4642
@Override
4743
public void onCreate() {
4844
super.onCreate();
4945

50-
receiverPushBroadcast();
51-
5246
initImageLoader(this);
5347

5448
sScale = getResources().getDisplayMetrics().density;
@@ -60,52 +54,6 @@ public void onCreate() {
6054

6155
sUserObject = AccountInfo.loadAccount(this);
6256
sUnread = new Unread();
63-
64-
}
65-
66-
public static final String PushClickBroadcast = "PushClickBroadcast";
67-
68-
private void receiverPushBroadcast() {
69-
IntentFilter filter = new IntentFilter(PushClickBroadcast);
70-
registerReceiver(new BroadcastReceiver() {
71-
@Override
72-
public void onReceive(Context context, Intent intent) {
73-
74-
String url = intent.getStringExtra("data");
75-
76-
if (url != null) {
77-
closeNotify(url);
78-
if (sMainCreate) {
79-
MainActivity.setJumpNewIntent();
80-
URLSpanNoUnderline.openActivityByUri(context, url, true);
81-
} else {
82-
Intent mainIntent = new Intent(context, MainActivity_.class);
83-
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
84-
mainIntent.putExtra("mPushUrl", url);
85-
context.startActivity(mainIntent);
86-
}
87-
}
88-
89-
String id = intent.getStringExtra("id");
90-
if (id != null && !id.isEmpty()) {
91-
AsyncHttpClient client = MyAsyncHttpClient.createClient(context);
92-
final String host = Global.HOST + "/api/notification/mark-read?id=%s";
93-
client.post(String.format(host, id), new JsonHttpResponseHandler() {
94-
});
95-
}
96-
}
97-
}, filter);
98-
}
99-
100-
private void closeNotify(String url) {
101-
String notifys[] = PushReceiver.sNotify;
102-
NotificationManager mNotificationManager = (NotificationManager)
103-
getSystemService(NOTIFICATION_SERVICE);
104-
for (int i = 0; i < notifys.length; ++i) {
105-
if (url.equals(notifys[i])) {
106-
mNotificationManager.cancel(i);
107-
}
108-
}
10957
}
11058

11159
public static void initImageLoader(Context context) {

0 commit comments

Comments
 (0)