apply plugin:'com.google.gms.google服务'
classpath'com.google。gms:google-services:4.3.3'
添加的依赖项
implementation'com.google.firebase:firebase-auth:19.2.0'
implementation'com.google.firebase:firebase-core:17.2.2'
implementation'com.google.firebase:firebase-crash:16.2.1'
implementation'com.google.firebase:firebase-messaging:20.1.0'
implementation'com.google.firebase:firebase-analytics:17.2.2'
implementation'androidx.legacy:legacy-support-v4:1.0.0'
implementation'com.google.android.gms:play-services-tagmanager:17.0.0'
myfirebaseinstanceidservice.java
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseIIDService";
public static String fcm_Tocken;
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(s);
storeRegIdInPref(token);
sendRegistrationToServer(token);
Intent registrationComplete = new Intent(CONSTANTS.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", token);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
FirebaseMessaging.getInstance().subscribeToTopic("all");
SharedPreferences.Editor editor1 = getSharedPreferences(CONSTANTS.API_param_DeviceToken, MODE_PRIVATE).edit();
editor1.putString(CONSTANTS.API_param_DeviceToken, refreshedToken); //Friend
editor1.apply();
editor1.commit();
fcm_Tocken = refreshedToken;
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}
private void storeRegIdInPref(String token) {
SharedPreferences.Editor editor1 = getSharedPreferences(CONSTANTS.API_param_DeviceToken, MODE_PRIVATE).edit();
editor1.putString(CONSTANTS.API_param_DeviceToken, token); //Friend
editor1.apply();
editor1.commit();
}
}
myfirebasemessagingservice.java文件
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private NotificationManager mNotificationManager;
private NotificationCompat.Builder notificationBuilder;
Activity context;
String title = "", image = "", message = "", flag = "", id = "";
TaskStackBuilder taskStackBuilder;
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
title = remoteMessage.getNotification().getTitle();
message = remoteMessage.getNotification().getBody();
sendNotification(title, message, flag, id, String.valueOf(m));
}
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
title = remoteMessage.getData().get("title");
image = remoteMessage.getData().get("image");
message = remoteMessage.getData().get("body");
flag = remoteMessage.getData().get("flag");
id = remoteMessage.getData().get("id");
sendNotification(title, message, flag, id, String.valueOf(m));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
private void sendNotification(String title, String message, String flag, String id, String m) {
Intent resultIntent = null;
PendingIntent resultPendingIntent = null;
taskStackBuilder = TaskStackBuilder.create(this);
try {
if (flag != null && flag.equalsIgnoreCase("friend")) {
resultIntent = new Intent(this, ShareAndEarnActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.putExtra(CONSTANTS.back_flag, CONSTANTS.FLAG_ONE);
resultIntent.putExtra(CONSTANTS.refer_a_freind, CONSTANTS.FLAG_ONE);
taskStackBuilder.addParentStack(NavigationActivity.class);
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
} else {
resultIntent = new Intent(this, NavigationActivity.class);
taskStackBuilder.addParentStack(NavigationActivity.class);
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("YupIt Notification");
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder = new NotificationCompat.Builder(this, notificationChannel.getId());
} else {
notificationBuilder = new NotificationCompat.Builder(this);
}
notificationBuilder.setSmallIcon(R.drawable.yupit_tranparent_icon);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
notificationBuilder.setContentTitle(title);
notificationBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationBuilder.setContentText(message);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL);
notificationBuilder.setColor(getResources().getColor(R.color.darkorange));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(resultPendingIntent);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.icon);
} else {
notificationBuilder.setSmallIcon(R.drawable.icon);
}
Notification note = notificationBuilder.build();
note.flags = Notification.FLAG_INSISTENT;
note.flags = Notification.DEFAULT_VIBRATE;
note.flags = Notification.DEFAULT_SOUND;
note.flags = Notification.DEFAULT_LIGHTS;
note.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(Integer.parseInt(m), note);
} catch (Exception e) {
e.printStackTrace();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!