第一次提交
This commit is contained in:
54
Mko888/src/main/java/com/web/MKO888/MainActivity2.java
Normal file
54
Mko888/src/main/java/com/web/MKO888/MainActivity2.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.web.MKO888;
|
||||
|
||||
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
import com.web.base.MainActivity;
|
||||
|
||||
public class MainActivity2 extends com.web.base.MainActivity2 {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
userId = 96;
|
||||
saveInt(MainActivity2.this,"user_code",userId);
|
||||
saveInt(MainActivity2.this,"version_code",getVersion());
|
||||
MainActivity.saveString(this, "base_url", "https://www.mko888.net/");
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
//订阅主题
|
||||
FirebaseMessaging.getInstance().subscribeToTopic("demo")
|
||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
String msg = "Subscribed";
|
||||
if (!task.isSuccessful()) {
|
||||
msg = "Subscribe failed";
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int getVersion(){
|
||||
try {
|
||||
PackageManager packageManager = getPackageManager();
|
||||
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
|
||||
String StringversionName = packageInfo.versionName; // 版本号
|
||||
int versionCode = packageInfo.versionCode; // 版本码
|
||||
// 在这里可以使用versionName和versionCode进行相关的操作
|
||||
// Log.d("VersionInfo", "VersionName: " + versionName + ", VersionCode: " + versionCode);
|
||||
return versionCode;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.web.MKO888;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
import com.google.gson.Gson;
|
||||
import com.web.base.GsonUtils;
|
||||
import com.web.base.MessageInfo;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class MyFirebaseMessageingService extends FirebaseMessagingService {
|
||||
|
||||
public MyFirebaseMessageingService() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
|
||||
super.onMessageReceived(remoteMessage);
|
||||
Map<String, String> serviceData = remoteMessage.getData(); //后台推送数据
|
||||
if (serviceData != null && serviceData.containsKey("message")) {
|
||||
String value = serviceData.get("message");
|
||||
Gson gson = new Gson();
|
||||
MessageInfo messageInfo = gson.fromJson(value, MessageInfo.class);
|
||||
showNotification(messageInfo);
|
||||
// if (remoteMessage.getNotification() != null) {
|
||||
// showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
|
||||
// }
|
||||
} else {
|
||||
//收到通知 创建notify
|
||||
if (remoteMessage.getNotification() != null) {
|
||||
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void showNotification(MessageInfo messageInfo) {
|
||||
Intent notifyIntent = new Intent(this, com.web.base.MainActivity2.class);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
|
||||
ComponentName launchComponent = null;
|
||||
launchComponent = getApplication()
|
||||
.getPackageManager()
|
||||
.getLaunchIntentForPackage(getApplication().getPackageName())
|
||||
.getComponent();
|
||||
notifyIntent.setComponent(launchComponent);
|
||||
}
|
||||
|
||||
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
notifyIntent.setAction(Intent.ACTION_VIEW);
|
||||
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
|
||||
notifyIntent.putExtra("message", messageInfo);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
// Intent notifyIntent = new Intent(this, MainActivity2.class);
|
||||
// notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
// // notifyIntent.putExtra("message", messageInfo);
|
||||
// notifyIntent.setAction(Intent.ACTION_VIEW);
|
||||
// notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
|
||||
// PendingIntent pendingIntent;
|
||||
// pendingIntent = PendingIntent.getActivity
|
||||
// (this, 0, notifyIntent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
NotificationChannel channelwinway = null;
|
||||
NotificationCompat.Builder notificationBuilder = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
channelwinway = new NotificationChannel(getString(com.web.base.R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
|
||||
channelwinway.enableLights(true);
|
||||
channelwinway.enableVibration(true);
|
||||
notificationManager.createNotificationChannel(channelwinway);
|
||||
notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
|
||||
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
|
||||
.setContentTitle(messageInfo.getTitle())
|
||||
.setContentText(messageInfo.getContent())
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
} else {
|
||||
notificationBuilder = new NotificationCompat.Builder(this, getString(com.web.base.R.string.app_name))
|
||||
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
|
||||
.setContentTitle(messageInfo.getTitle())
|
||||
.setContentText(messageInfo.getContent())
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
}
|
||||
notificationManager.notify(0, notificationBuilder.build());
|
||||
}
|
||||
|
||||
private void showNotification(String title, String body) {
|
||||
Intent notifyIntent = new Intent(this, MainActivity2.class);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
|
||||
ComponentName launchComponent = null;
|
||||
launchComponent = getApplication()
|
||||
.getPackageManager()
|
||||
.getLaunchIntentForPackage(getApplication().getPackageName())
|
||||
.getComponent();
|
||||
notifyIntent.setComponent(launchComponent);
|
||||
}
|
||||
notifyIntent.putExtra("message", body);
|
||||
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
notifyIntent.setAction(Intent.ACTION_VIEW);
|
||||
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
|
||||
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
NotificationChannel channelwinway = null;
|
||||
NotificationCompat.Builder notificationBuilder = null;
|
||||
MessageInfo messageInfo = GsonUtils.getObjFromJSON(body, MessageInfo.class);
|
||||
if (messageInfo != null) {
|
||||
body = messageInfo.getContent();
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
channelwinway = new NotificationChannel(getString(com.web.base.R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
|
||||
channelwinway.enableLights(true);
|
||||
channelwinway.enableVibration(true);
|
||||
notificationManager.createNotificationChannel(channelwinway);
|
||||
notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
|
||||
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
} else {
|
||||
notificationBuilder = new NotificationCompat.Builder(this, getString(com.web.base.R.string.app_name))
|
||||
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
}
|
||||
notificationManager.notify(0, notificationBuilder.build());
|
||||
//存储数据
|
||||
// saveNotifyMessage(body);
|
||||
}
|
||||
|
||||
|
||||
// public void saveNotifyMessage(String body) {
|
||||
// MessageInfo messageInfo = GsonUtils.getObjFromJSON(body, MessageInfo.class);
|
||||
// String savenotify = Utils.get(getApplication(),ApiService.savenotify,"");
|
||||
// if(messageInfo!=null){
|
||||
// if(TextUtils.isEmpty(savenotify)){
|
||||
// GsonUtils.getListFromJSON(savenotify,)
|
||||
// }
|
||||
// }
|
||||
// String jsonString = GsonUtils.beanToJSONString(chatMessageBeans);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param key 要设置的key
|
||||
// */
|
||||
// public static void set(Context activity, String key, String is) {
|
||||
// SharedPreferences nameSetting = getConfigShared(activity);
|
||||
// SharedPreferences.Editor namePref = nameSetting.edit();
|
||||
// namePref.putString(key, is);
|
||||
// namePref.commit();
|
||||
// }
|
||||
}
|
||||
37
Mko888/src/main/java/com/web/MKO888/WebApplication.java
Normal file
37
Mko888/src/main/java/com/web/MKO888/WebApplication.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.web.MKO888;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import com.tencent.smtt.export.external.TbsCoreSettings;
|
||||
import com.tencent.smtt.sdk.QbSdk;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class WebApplication extends Application {
|
||||
|
||||
|
||||
public static Context application;
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
// 设置开启优化方案
|
||||
application = this;
|
||||
HashMap map = new HashMap();
|
||||
map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
|
||||
map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
|
||||
QbSdk.initTbsSettings(map);
|
||||
QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
|
||||
@Override
|
||||
public void onCoreInitFinished() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewInitFinished(boolean b) {
|
||||
|
||||
}
|
||||
});
|
||||
QbSdk.setDownloadWithoutWifi(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user