有几条通知,然后单击我要转到特定活动的通知。假设出现奖励通知,然后单击将转到RewardActivity并添加新朋友,然后单击newFriend通知将转到friendlistActivity,无论该应用程序是前台还是后台。
但对于我来说,如果出现任何通知,它将转到同一活动而不是不同的活动。
private void handleDataMessage(String noti_title,String noti_message,String noti_click_action) {
try {
Intent intent = new Intent(this, RewardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", noti_title);
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
//image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
//intent.putExtra("img", image);
intent.putExtra("msg", noti_message);
intent.putExtra("click_action", noti_click_action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this, "Default")
//.setLargeIcon(R.mipmap.ic_launcher)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher)
//.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("Default")
.setVibrate(new long[]{1000, 1000})
.setContentIntent(pendingIntent);
notificationBuilder.setContentTitle(noti_title);
notificationBuilder.setContentText(noti_message);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
答案 0 :(得分:1)
FCM代表客户端应用程序自动向最终用户设备显示消息。当用户单击通知时,将创建两个条件
您的应用在后台运行时发送的通知。在这种情况下,通知将传递到设备的系统托盘。用户点击通知会默认打开应用启动器。
同时具有通知和数据有效负载(背景和前景)的邮件。在这种情况下,通知会传递到 设备的系统托盘,并且数据有效载荷是在启动器活动的意图范围内提供的。
如果您要在应用程序处于后台且通知仅包含通知且没有数据播放的情况下打开所需的活动,则不可能。
但是,如果要在应用程序处于后台且通知包含数据播放量时打开所需活动,则可以将用户导航至所需活动。
请参阅下面的示例,其中包含具有背景和前景条件的数据有效负载的消息时,打开所需活动。
我的AndroidManifest启动器中的活动是SplashActivity
<activity android:name=".activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
您可以通过Google FCM Tester在pushtry.com上测试通知。 这是通过pushtry.com发送时的播放负载数据的格式
{
"to":"your_device_token",
"data": {
"title": "hello",
"message": "test message",
},
"priority":"high"
}
MyFirebaseMessagingService类:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
private static int count = 0;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
KeyManager.setSharedPreferenceString(getApplicationContext(), "fcm_token", s);
Log.e(TAG, "onNewToken: " + s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Here notification is recieved from server
try {
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
//you can use your launcher Activity insted of SplashActivity, But if the Activity you used here is not launcher Activty than its not work when App is in background.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Add Any key-value to pass extras to intent
intent.putExtra("pushnotification", "yes");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//For Android Version Orio and greater than orio.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel("Sesame", "Sesame", importance);
mChannel.setDescription(messageBody);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotifyManager.createNotificationChannel(mChannel);
}
//For Android Version lower than orio.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "Seasame");
mBuilder.setContentTitle(title)
.setContentText(messageBody)
.setSmallIcon(R.mipmap.ic_launcher_sesame)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_sesame))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(Color.parseColor("#FFD600"))
.setContentIntent(pendingIntent)
.setChannelId("Sesame")
.setPriority(NotificationCompat.PRIORITY_LOW);
mNotifyManager.notify(count, mBuilder.build());
count++;
}
}
现在,当您在后台收到推送通知并单击通知时,通知将传递到 设备的系统托盘和其他功能会传递到启动器活动中。 检查启动器活动是否为多余或为空,然后将用户导航到所需的活动。
如果您希望仅向登录用户显示活动,则SplashActivity如下所示:
public class SplashActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
try {
if (KeyManager.getSharedPreferenceBoolean(SplashActivity.this, "isLoggedIn", false)) {
if (getIntent().hasExtra("pushnotification")) {
Intent intent = new Intent(this, YourDesiredActivity.class);
startActivity(intent);
finish();
} else {
CheckLogin();
}
} else {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
} catch (Exception e) {
CheckLogin();
e.printStackTrace();
}
}
private void CheckLogin() {
if (KeyManager.getSharedPreferenceBoolean(SplashActivity.this, "isLoggedIn", false)) {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, 2500);
}
}
}
SplashActivty中的逻辑,用于根据条件在不同的“活动”中导航用户。
当用户以正常流程打开应用程序而不点击pushnotification时,getIntent()。hasExtra(“ pushnotification”)为null,因此命令转到catch块,并检查CheckLogin()方法是否已登录。 但是,通过单击pushnotification输入的用户,则getIntent()。hasExtra(“ pushnotification”)不为null,他将转到所需的Activity。
答案 1 :(得分:1)
我遇到了同样的问题,经过 doc 后,这两个步骤对我有用。
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
完整代码:
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
"Foreground_Service_Channel_Id",
"Foreground_Service_Channel_Name",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getApplicationContext().getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
private void showNotification()
{
Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(notificationIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
Notification.Builder builder = new Notification.Builder(context, "Foreground_Service_Channel_Id")
.setSmallIcon(R.drawable.ic_mode_night_24)
.setContentTitle("SAMPLE_TEXT_TITLE")
.setContentIntent(resultPendingIntent)
.setStyle(new Notification.InboxStyle()
.setSummaryText("SAMPLE_TEXT"))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOngoing(true)
.setAutoCancel(false);
Notification notification = builder.build();
startForeground(ID, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mode_night_24)
.setContentTitle("SAMPLE_TEXT_TITLE")
.setContentIntent(resultPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOngoing(true)
.setAutoCancel(false);
Notification notification = builder.build();
startForeground(ID, notification);
}
}