【发布时间】:2018-09-08 11:48:49
【问题描述】:
以下是消息服务的代码。
public class FireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static int count = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "Notification Message TITLE: " +
remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message BODY: " +
remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification Message DATA: " +
remoteMessage.getData().toString());
sendNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody(),
remoteMessage.getData());
}
//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody,
Map<String, String> row) {
// PendingIntent contentIntent = null;
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this,
LoginActivity.class), 0);
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher_foreground))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count, notificationBuilder.build());
count++;
}
}
我是编程新手,所以请忽略错误。此外,在上面的代码中,当应用程序处于前台时它不会通知。所以有两个查询,一个是单击通知时它不会触发 LoginActivity 和其他是应用程序在前台时没有通知。
【问题讨论】:
-
LoginActivity是您的启动器活动吗?您是否在清单中注册了服务FireBaseMessagingService?您是否在使用 Android Oreo 的设备中测试您的代码? -
您真的有来自
activity或context的参考吗? -
@NabinBhandari是的,我已经在清单中注册它并在奥利奥进行测试
-
@jake 我没听懂你吗?
标签: android firebase android-intent push-notification firebase-cloud-messaging