单击通知时单击事件不起作用
Posted
技术标签:
【中文标题】单击通知时单击事件不起作用【英文标题】:Click event not working when notification clicked 【发布时间】:2018-09-08 11:48:49 【问题描述】:以下是消息服务的代码。
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 和其他是应用程序在前台时没有通知。
【问题讨论】:
LoginActivity
是您的启动器活动吗?您是否在清单中注册了服务FireBaseMessagingService
?您是否在使用 android Oreo 的设备中测试您的代码?
您真的有来自activity
或context
的参考吗?
@NabinBhandari是的,我已经在清单中注册它并在奥利奥进行测试
@jake 我没听懂你吗?
【参考方案1】:
我认为您缺少以下行 **
notificationManager.contentIntent = contentIntent;
**
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.contentIntent = contentIntent;
notificationManager.notify(count, notificationBuilder.build());
【讨论】:
【参考方案2】:您正在使用什么类型的通知? 在您的通知中,您只需添加点击操作方法并发送它
'
<intent-filter>
<action android:name="given click ation name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>'
这是更好的学习方式
【讨论】:
以上是关于单击通知时单击事件不起作用的主要内容,如果未能解决你的问题,请参考以下文章