我有这个实现服务的类:
public class myFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
}
}
现在来自另一个班级(事实上是另一个班级)我想打电话给onMessageReceived(RemoteMessage remoteMessage)
,但我不知道该怎么做..这可能吗?
答案 0 :(得分:1)
没有必要这样做,基本上你会向自己发送通知。
如果这是你的目的,只需在你要调用onMessageReceived()的地方创建通知;
答案 1 :(得分:1)
我有类似的问题。我使用相同的intent过滤器有两个服务。为了解决这个问题,我决定创建第三个服务,订阅了intent过滤器。在这项服务中,我实例化了每个服务,并将收到的消息传递给他们。如果您只是实例化服务的问题与Context
相关,因为您没有。
检查服务中的所有方法我看到你有一个受保护的方法attachBaseContext(context);
所以我创建了一个公共方法来设置我的包装器服务的基本上下文,如下所示:
public class WrapperService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage message) {
Service1 service1 = new Service1(getBaseContext());
service1.onMessageReceived(message);
Service2 = new Service2(getBaseContext());
service2.onMessageReceived(message);
}
}
现在,对于每项服务,我都有类似的内容:
public class Service1 extends FirebaseMessagingService {
public Service1(Context context) {
super.attachBaseContext(context);
}
public void onMessageReceived(RemoteMessage message) {
....
}
}
通过这些更改,我可以使用相同的intent过滤器调用这两个服务。
最后的想法是,如果由于它们包含在第三方库中而无法访问服务,则应检查是否可以从该服务扩展类并按顺序向该类添加公共构造函数初始化基本上下文。
答案 2 :(得分:0)
如果您想从其他活动中调用onMessageReceived()
,请尝试使用此代码。
myFirebaseMessagingService test = new myFirebaseMessagingService();
test.onMessageReceived(); //You have to pass remoteMessage in the parameter
虽然很奇怪,你为什么要从活动中调用onMessageReceived()
?
答案 3 :(得分:0)
如果您只是想创建通知,只需在您自己的服务中使用通知构建器
答案 4 :(得分:0)
首先,FCM用于push Notification。不用于内部交流。
<service android:name=".fcms.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
以及
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
val TAG :String = "MessagingService"
override fun onNewToken(str: String?) {
super.onNewToken(str)
//send token to server... if user is logged In...
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
/*Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());*/
// Check if message contains a data payload.
if (remoteMessage?.data!!.size > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
/*Log.e(TAG, "collapsekey: " + remoteMessage.getCollapseKey());
Log.e(TAG, "from: " + remoteMessage.getFrom());
Log.e(TAG, "message id: " + remoteMessage.getMessageId());
Log.e(TAG, "message type:: " + remoteMessage.getMessageType());
Log.e(TAG, "to: " + remoteMessage.getTo());
Log.e(TAG, "send time: " + remoteMessage.getSentTime());
Log.e(TAG, "ttl: " + remoteMessage.getTtl());
Log.e(TAG, "title: " + remoteMessage.getNotification().getTitle());
Log.e(TAG, "body: " + remoteMessage.getNotification().getBody());
Log.e(TAG, "click action: " + remoteMessage.getNotification().getClickAction());
Log.e(TAG, "color: " + remoteMessage.getNotification().getColor());
Log.e(TAG, "icon: " + remoteMessage.getNotification().getIcon());*/
}
// Check if message contains a notification payload.
if (remoteMessage.notification != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.notification!!.body!!)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val NOTIFICATION_CHANNEL_ID = "broadpeak.notification"
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val name = "my_channel"
val Description = "This is my channel"
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = Description
mChannel.enableLights(true)
mChannel.lightColor = Color.RED
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mChannel.setShowBadge(false)
notificationManager.createNotificationChannel(mChannel)
}
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
val tagID = remoteMessage?.data!!.get("tagID").toString()
val tagName = remoteMessage?.data!!.get("tagName").toString()
val intent = Intent(this, McqActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// | Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("keyTagBO",SubjectBO(tagID,tagName))
val resultIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val startIntent = PendingIntent.getService(this,0,
Intent(this,BackgroundService::class.java).setAction("START"),PendingIntent.FLAG_UPDATE_CURRENT)
val stopIntent = PendingIntent.getService(this,0,
Intent(this,BackgroundService::class.java).setAction("STOP"),PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.round_green_layout)
.setOngoing(true)//If you want not to remove it on Swipe
.setContentTitle(remoteMessage.notification!!.title)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(longArrayOf(0, 1000, 500, 1000))
.setContentText(remoteMessage.notification!!.body)
.setContentIntent(resultIntent)
.addAction(R.mipmap.ic_launcher,"Start Action",startIntent)
.addAction(R.mipmap.ic_launcher,"Stop Action",stopIntent)
.setContentInfo("Info")
notificationManager.notify(Random().nextInt(), notificationBuilder.build())
}
}
转到firebase控制台并选择“云消息传递”并创建一个通知..并根据需要传递键值对
答案 5 :(得分:0)
尝试一下...
RemoteMessage remoteMessage = new RemoteMessage.Builder(<ANY_STRING>)
.addData(<YOUR_KEY_HERE>, <VALUE>)
.build();
PushReceiver pushReceiver = new PushReceiver(getContext());
pushReceiver.onMessageReceived(remoteMessage);