我使用 Firebase云消息传递从服务器向移动设备发送消息,因此我创建了一个从FirebaseMessagingService
扩展并覆盖onMessageReceived(RemoteMessage remoteMessage)
的类我正在调用已实现的方法notifyUser(String from, String notification)
。反过来,此方法调用属于类showNotification(String from, String notification, Intent intent)
的另一个方法myNotificationManager
,我在其中创建通知。问题是我需要启动一个片段而不是一个活动。有什么想法怎么做?
提前谢谢。
public class ServicioFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
notifyUser(remoteMessage.getFrom(),remoteMessage.getNotification().getBody());}
public void notifyUser(String from, String notification){
//Create an object of the class that contain the method which creates the notification
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
//Create an Intent that will be passed as a parameter in the method that creates the
//notification. This intent will be executed when user clicks on the notification. ShowMessage is a class in which
//I define some TextView and other elements to show the message
Intent intent = new Intent(getApplicationContext(),ShowMessage.class);
//put some values in the intent
//.......................
//calls the method that creates the notification
myNotificationManager.showNotification(from,notification,intent);
}
}
班级MyNotificationManager
的代码:
public class MyNotificationManager {
//Activity context
private Context ctx;
//Notification id.
public static final int NOTIFICATION_ID = 234;
//Sound for notification
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//Constructor
public MyNotificationManager(Context ctx) {
this.ctx = ctx;
}
public void showNotification (String from, String notification, Intent intent){
//Define an Intent and actions to perform when called
PendingIntent pendingIntent = PendingIntent.getActivity(
ctx,
NOTIFICATION_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//To configre the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Notification mNotification = builder.setSmallIcon(R.mipmap.ic_launcher)
//define notification parameters
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(from)
.setContentText(notification)
.setSound(uri)
.setSmallIcon(R.mipmap.ic_face)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(),R.mipmap.ic_face))
.build();
//Define notification flags
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
//Notify user about received message
NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
//Execute notification
notificationManager.notify(NOTIFICATION_ID,mNotification);
}
}//end of class
答案 0 :(得分:0)
我找到了办法。我调用MainActivity.class而不是让Intent调用ShowMessage.class。现在,MainActivity(一般应用程序)可以通过两种方式启动:
默认访问应用:用户点按应用的图标并访问应用(MainActivity)。
用户点击通知并通过在ServicioFirebaseMessaging类中扩展FirebaseMessagingService的notifyUser(String from,String notification)方法上实现的Intent访问应用程序(MainActivity)。在这个Intent中,我传递了一些将在MainActivity上使用的值。
现在的不同之处在于我已经在MainActivity中实现了额外的操作,被try / catch和'if'条件所包围,如果用户以第一种方式访问应用程序,则没有执行Intent然后没有从Intent获取的数据因为它甚至不存在(getIntent()。getExtras.getSomething()将失败),因为这个操作被try / cath所包围,app不会掉线。如果用户以第二种方式访问应用程序,那么try / catch中的操作将发生,因为'if'条件将为真,并且在这组操作中执行对相关片段的调用(FragmentSelectedMessage),传递给片段意图的值。当然,现在片段有一个支持它的活动。