firebase工作正常,在状态栏上推送通知但我的挑战是点击通知时,我希望它带我到我的自定义活动而不是默认启动器,我该怎么办?
class = "glyphicon glyphicon-eye-close glyphicon-eye-open"
这是我想要在通知时钟时打开的自定义活动。通知来自Firebase云消息传递。
class="glyphicon glyphicon-eye-open"
}
这是从firebase收听通知的服务。
答案 0 :(得分:2)
private void showNotification(String msg) {
//Creating a notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent().setClassName("packagename", "packagename.YourActivityname"); // give any activity name which you want to open
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setContentTitle("FireBase");
builder.setContentText(msg);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
我希望它可以帮到你!
答案 1 :(得分:0)
试试这个
Intent notificationIntent =new Intent(context,ActionActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
this.notification.setLatestEventInfo(context, title, message, intent);
this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
// you can also add other property like vibrate or sound
notificationManager.notify(0, notification);
答案 2 :(得分:0)
FirebaseMessagingService内部......
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
NotificationPublisher.getInstance().showNotification(this, remoteMessage);
}
然后在你的NotificationPublisher ...
public static final String EXTRA_CHAT_NOTIFICATION = "com.myProject.chat_notification";
public void showNotification(Context context, RemoteMessage remoteMessage) {
Intent intent = new Intent();
if (remoteMessage.getData().get("notification_type") != null) {
Intent i = new Intent("broadCastName");
i.putExtra("type", remoteMessage.getData().get("notification_type"));
i.putExtra("trip_id", remoteMessage.getData().get("trip_id"));
i.putExtra("status", remoteMessage.getData().get("status"));
context.sendBroadcast(i);
return;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent = new Intent(context, MainActivity.class);
HashMap<String, String> dataHash = new HashMap<>(remoteMessage.getData());
intent.putExtra(EXTRA_CHAT_NOTIFICATION, dataHash);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_noti)
.setLargeIcon(R.drawable.ic_noti)
.setColor(ContextCompat.getColor(context, R.color.white))
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("content"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
最后在你的MainActivity onCreate()处理传入的数据......
// Handle possible data accompanying notification message.
// [START handle_data_extras]
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
if(key.equals(NotificationPublisher.EXTRA_CHAT_NOTIFICATION)) {
// TODO: Start myCustomActivity
}
}
}
答案 3 :(得分:-1)
我找到了答案。它只是简单地在我的启动器屏幕上添加此代码,并在关闭启动器屏幕并转到预期活动时保存数据。
//When Notification is tapped
if (getIntent().getExtras() != null) {
//init message
String message = String.valueOf(getIntent().getExtras().get("message"));
String title = String.valueOf(getIntent().getExtras().get("title"));
//save the message
MySharedPreference.save(getApplicationContext() , "message" , message);
MySharedPreference.save(getApplicationContext() , "title" , title);
startActivity(new Intent(getApplicationContext() , MainActivity.class));
finish();
}