我正在Android项目中编写推送通知,在MyFirebaseMessagingService
服务中编写此代码以显示通知:
Intent intent = new Intent(getApplicationContext(), NotificationMessage.class); //
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(contentIntent);
mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
当主要活动显示,并发送推送,通知显示并点击重定向到NotificationMessage
活动,但当我关闭主要活动,并再次发送推送时,通知显示给我,但是当我点击它,重定向到主要活动并且不显示NotificationMessage
活动,会发生什么?我该如何解决这个问题?谢谢。
答案 0 :(得分:0)
试试这个:
private void setNotification(String notificationMessage) {
mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), NotificationMessage.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setStyle(new
NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(contentIntent);
mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
}
答案 1 :(得分:0)
public class MainActivity extends AppCompatActivity {
NotificationManager mNotificationManager;
Button btn_notifcation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_notifcation= (Button) findViewById(R.id.btn);
btn_notifcation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNotification();
}
});
}
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, NotificationMessage.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
答案 2 :(得分:0)
private void generateNotification(Context context, String message) {
int icon = R.mipmap.ic_launcher;
long when = System.currentTimeMillis();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
String title = context.getString(R.string.app_name);
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setLargeIcon(bitmap)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setNumber(messageCount)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notificationBuilder.build());
}