点击通知后,点击事件不起作用

时间:2018-09-08 11:48:50

标签: android firebase android-intent push-notification firebase-cloud-messaging

以下是消息传递服务的代码。

public class FireBaseMessagingService extends FirebaseMessagingService  {
private static final String TAG = "MyFirebaseMsgService";
private static int count = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "Notification Message TITLE: " + 
 remoteMessage.getNotification().getTitle());
    Log.d(TAG, "Notification Message BODY: " + 
 remoteMessage.getNotification().getBody());
    Log.d(TAG, "Notification Message DATA: " + 
remoteMessage.getData().toString());


    sendNotification(remoteMessage.getNotification().getTitle(),
            remoteMessage.getNotification().getBody(), 
 remoteMessage.getData());

}

//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody, 
 Map<String, String> row) {
    // PendingIntent contentIntent = null;
    PendingIntent contentIntent =
            PendingIntent.getActivity(this, 0, new Intent(this, 
  LoginActivity.class), 0);

    Uri defaultSoundUri = 
   RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new 
 NotificationCompat.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
  R.drawable.ic_launcher_foreground))
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(contentIntent);

    NotificationManager notificationManager =
            (NotificationManager) 
  getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(count, notificationBuilder.build());

    count++;

}
}

我是编程新手,请忽略错误。在上面的代码中,当应用程序处于前台时它也不会通知。所以有两个查询,一个是单击通知时它不会触发LoginActivity,另一个是应用程序在前台时会通知它。

2 个答案:

答案 0 :(得分:0)

我认为您在行下方失踪了                 **

  

notificationManager.contentIntent = contentIntent;

**

    NotificationManager notificationManager =
            (NotificationManager) 
  getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.contentIntent = contentIntent;
        notificationManager.notify(count, notificationBuilder.build());

答案 1 :(得分:0)

您使用的是哪种类型的通知? 在通知中,您只需添加click action方法并发送

'

        <intent-filter>

            <action android:name="given click ation name" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>' 

这是更好地学习课堂的好方法