【发布时间】:2021-02-16 08:12:15
【问题描述】:
我想在应用从最近的任务中滑出时推送通知 首先尝试我使用邮递员推送通知,但通知不显示。 第二次尝试我使用firebase控制台推送通知,并显示通知,然后我尝试使用邮递员推送并且它工作。 我的问题为什么如果我只使用邮递员推送它不起作用,但如果我使用firebase控制台触发然后使用邮递员它可以工作?如何在不从 Firebase 控制台触发的情况下推送?
这是我的安卓代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.d("TAG", "New token: " + token);
storeRegIdInPref(token);
}
private void storeRegIdInPref(String token) {
SharedPreferences sp = getApplicationContext().getSharedPreferences("FCMID", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("regId", token);
editor.commit();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
if (remoteMessage.getData()!= null)
try {
Map<String, String> data = remoteMessage.getData();
PushNotificationModel pushNotificationModel = PushNotificationModel.fromMap(data);
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
resultIntent.putExtra("Title", pushNotificationModel.title);
resultIntent.putExtra("Message", pushNotificationModel.message);
resultIntent.putExtra("NotifId", 2020090001);
showNotificationMessage(getApplicationContext(), pushNotificationModel.title, pushNotificationModel.message, "2020-09-29 17:45:22", resultIntent, 2020090001);
} catch (Exception e) {
e.printStackTrace();
}
/*if (remoteMessage == null)
return;
if (remoteMessage.getData().size() > 0)
{
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
}
}
else if (remoteMessage.getNotification() != null) {
handleNotification(remoteMessage.getNotification().getBody() , remoteMessage.getNotification().getTitle());
}*/
}
private void handleNotification(String message , String Title) {
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
showNotificationMessage(getApplicationContext(), Title, message, "" , resultIntent,0);
}
private void handleDataMessage(JSONObject json) {
try {
String title = json.getString("title");
String message = json.getString("message");
String timestamp = json.getString("timestamp");
String notifid = json.getString("notifid");
int id = Integer.parseInt(notifid);
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
resultIntent.putExtra("Title", title);
resultIntent.putExtra("Message", message);
resultIntent.putExtra("NotifId", notifid);
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent, id);
}
catch (Exception e) {
}
}
public void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent , int notifid) {
/*PendingIntent pendingIntent;
try {
pendingIntent = PendingIntent.getActivity(context, 60002, intent, PendingIntent.FLAG_ONE_SHOT);
} catch (Exception e) {
e.printStackTrace();
pendingIntent = null;
}
Bitmap Images;
String channelId = "channel-01";
String channelName = "Channel Name";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
*//*NotificationCompat.Builder notificationBuilderWithImage = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(Images).setSummaryText(message))
.setAutoCancel(true)
.setSound(defaultSoundUri);*//*
NotificationCompat.Builder notificationBuilderWithText = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo))
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setAutoCancel(true)
.setSound(defaultSoundUri);
if (pendingIntent != null) {
notificationBuilderWithText.setContentIntent(pendingIntent);
*//*if(Images != null){
notificationBuilderWithImage.setContentIntent(pendingIntent);
} else {
notificationBuilderWithText.setContentIntent(pendingIntent);
}*//*
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
*//*if(Images != null){
notificationManager.notify(0 *//**//* ID of notification *//**//*, notificationBuilderWithImage.build());
} else {
notificationManager.notify(0 *//**//* ID of notification *//**//*, notificationBuilderWithText.build());
}*//*
notificationManager.notify(0 *//* ID of notification *//*, notificationBuilderWithText.build());*/
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = notifid;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
}
}
这是我的邮递员代码
{
"data": {
"message": "\"Henlo6\"",
"notifid": 2020090001,
"timestamp": "\"2020-09-29 17:45:22\"",
"title": "\"Hai\"",
"priority":"HIGH"
},
"time_to_live": 86400,
"to": "token"
}
【问题讨论】:
标签: java android firebase background firebase-cloud-messaging