我正在使用FCM和PHP来发送通知。
应用位于前景,背景被杀死(在oreo之下)
应用已在 前景,背景和未杀死(在奥利奥中)
我什至尝试创建Notification Channel并尝试从php脚本中删除'notification',但是没有任何效果。我已经尝试了SO中所有重复的答案,但是没有任何效果。任何帮助都会很棒。谢谢。 这是我的代码,
@SuppressLint("NewApi")
private void sendNotification1(RemoteMessage remoteMessage) {
Log.e("remoteMessage", remoteMessage.getData().toString());
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title, body, pendingIntent, defaultsound, String.valueOf(R.drawable.appicon3copy));
int i = 0;
oreoNotification.getManager().notify(i, builder.build());
}
这是OreoNotification类,
public class OreoNotification extends ContextWrapper {
private static final String CHANNEL_ID = "Fcm Test";
private static final String CHANNEL_NAME = "Fcm Test";
private NotificationManager notificationManager;
public OreoNotification(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Fcm Test channel for app test FCM");
channel.enableLights(true);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (notificationManager == null) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return notificationManager;
}
@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getOreoNotification(String title, String body, PendingIntent pendingIntent, Uri soundUri, String icon) {
return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
.setAutoCancel(true)
.setSmallIcon(R.drawable.appicon3copy)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent);
}
}
这是我的PHP脚本的一部分,
$msg = array
(
'body' => utf8_encode('Firebase Push Notification'),
'title' => utf8_encode('Anusha Kumar'),
'click_action' => ('.SplashActivity'),
);
$fields = array
(
'to' => $_REQUEST['token'],
'notification' => $msg , // tried removing this line too but doesn't work
'data'=>$msg,
);