Не удалось подключить мобильное устройство к Android Studio
Моя проблема в том, что я хочу реализовать push-уведомления в моем приложении для Android. Я получил уведомление, но когда я нажимаю на уведомление, то моя деятельность не может быть запущена, пожалуйста, решите мою проблему. Весь код работает хорошо, но нажатие на уведомление не работает. Пожалуйста, просмотрите мой код и предложите мне решение для кода. Мой код для уведомления
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
Log.e("NEW_TOKEN", s);
}
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
String message = remoteMessage.getData().get("message");
String value = remoteMessage.getData().get("value");
String imageUri = remoteMessage.getData().get("image");
bitmap = getBitmapfromUrl(imageUri);
sendNotification(message, bitmap,value);// sendNotification(message, bitmap, TrueOrFlase);
}
private void sendNotification(String messageBody, Bitmap image,String value) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("value", value);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_web_client_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo2)
.setContentTitle("iTrue Latest Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(Color.RED, 5000, 5000)
.setColor(Color.RED)
.setSubText("Watch Latest Notification")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
/*
*To get a Bitmap image from the URL received
* */
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
и мой сервис запуска для уведомления:
<service
android:name=".MyFirebaseMessagingService"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
1 ответ
Решение
Откройте настройки своего мобильного телефона и перейдите к варианту разработчика и нажмите на него 7 раз, возможно, это поможет вам подключиться.
Вам нужно добавить флаги в Intent. Я думаю, у вас меньше версии Oreo или AP
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);