- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遵循本教程 Lapit Chat App - Push Notifications - Firebase Tutorials - Part 24
一切顺利,只是当通知到来时我发现了一点问题,我的设备没有运行应用程序(后台模式),当我点击通知面板时,我的应用程序崩溃了。
如果我的设备之前打开过该应用程序,则不会出现该错误。当我点击通知面板时,应用程序正常运行并打开我的 ProfileActivity
我得到这个错误的 ErrorLog
07-29 16:36:54.512 16573-16573/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bertho.chat, PID: 16573
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bertho.chat/com.bertho.chat.ProfileActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(Unknown Source)
at com.bertho.chat.ProfileActivity.onCreate(ProfileActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
这是我的 ProfileActivity.java
public class ProfileActivity extends BaseActivity {
private ImageView mProfileImage;
private TextView mProfileName, mProfileStatus, mProfileFriendsCount;
private Toolbar mToolbar;
private Button mProfileSendReqBtn, mProfileDeclineReqBtn;
private ProgressDialog mDialog;
private FirebaseUser mCuurentUser;
private String mCuurent_state;
private DatabaseReference mUserDatabase, mFriendsReqDatabase, mFriendsDatabase, mNotificationDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_profile );
final String user_id = getIntent().getStringExtra( "user_id" );
mDialog = new ProgressDialog( this );
mUserDatabase = FirebaseDatabase.getInstance().getReference().child( "Users" ).child( user_id );
mUserDatabase.keepSynced( true );
mFriendsReqDatabase = FirebaseDatabase.getInstance().getReference().child( "FriendsRequest" );
mFriendsReqDatabase.keepSynced( true );
mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child( "FriendsData" );
mFriendsDatabase.keepSynced( true );
mNotificationDatabase = FirebaseDatabase.getInstance().getReference().child( "Notifications" );
mProfileImage = (ImageView) findViewById( R.id.profile_image );
mProfileName = (TextView) findViewById( R.id.profile_displayName );
mProfileStatus = (TextView) findViewById( R.id.profile_status );
mProfileFriendsCount = (TextView) findViewById( R.id.profile_totalFriends );
mProfileSendReqBtn = (Button) findViewById( R.id.profile_send_fr_btn );
mProfileDeclineReqBtn = (Button) findViewById( R.id.profile_decline_req_btn );
mCuurentUser = FirebaseAuth.getInstance().getCurrentUser();
showLoading( "Load Profile Data" );
mCuurent_state = "not_friends";
mUserDatabase.addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String display_name = dataSnapshot.child( "name" ).getValue().toString();
String display_status = dataSnapshot.child( "status" ).getValue().toString();
final String display_image = dataSnapshot.child( "image" ).getValue().toString();
mProfileName.setText( display_name );
mProfileStatus.setText( display_status );
if (mCuurent_state == "not_friends") {
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
} else if (mCuurent_state == "received") {
mProfileDeclineReqBtn.setVisibility( View.VISIBLE );
mProfileDeclineReqBtn.setEnabled( true );
} else {
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
}
/*Picasso.with( ProfileActivity.this )
.load( display_image )
.placeholder( R.drawable.no_profile )
.into( mProfileImage );*/
Picasso.with( ProfileActivity.this )
.load( display_image )
.networkPolicy( NetworkPolicy.OFFLINE )
.placeholder( R.drawable.no_profile )
.into( mProfileImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with( ProfileActivity.this )
.load( display_image )
.placeholder( R.drawable.no_profile )
.into( mProfileImage );
}
} );
// ========== FRIEND LIST ONLY FOR CURRENT USER LOGGED IN ==========
mFriendsReqDatabase.child( mCuurentUser.getUid() ).addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild( user_id )) {
String req_type = dataSnapshot.child( user_id ).child( "request_type" ).getValue().toString();
if (req_type.equals( "received" )) {
mCuurent_state = "req_received";
mProfileSendReqBtn.setText( "Accept Friend Request" );
mProfileDeclineReqBtn.setVisibility( View.VISIBLE );
mProfileDeclineReqBtn.setEnabled( true );
} else if (req_type.equals( "sent" )) {
mCuurent_state = "req_sent";
mProfileSendReqBtn.setText( "Cancel Friend Request" );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
}
} else {
/*Toast.makeText(getApplicationContext(), "TIDAK ADA : " + mCuurent_state, Toast.LENGTH_SHORT).show();*/
mFriendsDatabase.child( mCuurentUser.getUid() ).addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild( user_id )) {
mCuurent_state = "friends";
mProfileSendReqBtn.setText( "Unfriend This Person" );
/*mProfileSendReqBtn.setEnabled( false );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );*/
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
mDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
mProfileSendReqBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
// ========== ADD FRIEND REQUEST ==========
if (mCuurent_state.equals( "not_friends" )) {
showLoading( "Data is processing...!" );
mProfileSendReqBtn.setEnabled( false );
mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).child( "request_type" ).setValue( "sent" ).addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).child( "request_type" ).setValue( "received" ).addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
HashMap<String, String> notificationData = new HashMap<>( );
notificationData.put( "from", mCuurentUser.getUid() );
notificationData.put( "type", "request" );
mNotificationDatabase.child( user_id ).push().setValue( notificationData ).addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mDialog.dismiss();
mCuurent_state = "req_sent";
mProfileSendReqBtn.setText( "Cancel Friend Request" );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
showSuccess( "Request Sent!" );
}
} );
} else {
mDialog.dismiss();
onError( "Request Submission Failed!" );
}
}
} );
} else {
mDialog.dismiss();
onError( "Request Submission Failed!" );
}
mProfileSendReqBtn.setEnabled( true );
}
} );
}
// ========== CANCEL REQUEST FRIEND ==========
if (mCuurent_state.equals( "req_sent" )) {
showLoading( "Data is processing...!" );
mProfileSendReqBtn.setEnabled( false );
mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mDialog.dismiss();
mCuurent_state = "not_friends";
mProfileSendReqBtn.setText( "Send Friend Request" );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.successAlert ) );
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
onSuccess( "Cancel Request Success!" );
} else {
mDialog.dismiss();
onError( "Cancel Request Failed!" );
}
}
} );
} else {
mDialog.dismiss();
onError( "Cancel Request Failed!" );
}
mProfileSendReqBtn.setEnabled( true );
}
} );
}
// ========== ACTION APPROVE REQUEST ==========
if (mCuurent_state.equals( "req_received" )) {
showLoading( "Data is processing...!" );
mProfileSendReqBtn.setEnabled( false );
final String currentDate = DateFormat.getDateTimeInstance().format( new Date() );
mFriendsDatabase.child( mCuurentUser.getUid() ).child( user_id ).setValue( currentDate ).addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFriendsDatabase.child( user_id ).child( mCuurentUser.getUid() ).setValue( currentDate ).addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mDialog.dismiss();
mCuurent_state = "friends";
mProfileSendReqBtn.setText( "Unfriend This Person" );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );
mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
mProfileDeclineReqBtn.setEnabled( false );
showSuccess( "Success approve friend request!" );
} else {
mDialog.dismiss();
onError( "Failed approve friend request!" );
}
}
} );
} else {
mDialog.dismiss();
onError( "Failed approve friend request!" );
}
mProfileSendReqBtn.setEnabled( true );
}
} );
}
} );
} else {
mDialog.dismiss();
onError( "Failed approve friend request!" );
}
mProfileSendReqBtn.setEnabled( true );
}
} );
}
// ========== ACTION REMOVE FRIENDS ==========
if (mCuurent_state.equals( "friends" )) {
showLoading( "Data is processing...!" );
mFriendsDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mDialog.dismiss();
mCuurent_state = "not_friends";
mProfileSendReqBtn.setText( "Send Friend Request" );
mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.successAlert ) );
}
} );
}
}
} );
}
private void showLoading(String s) {
mDialog.setTitle( "Please wait a moment" );
mDialog.setMessage( s );
mDialog.setCanceledOnTouchOutside( false );
mDialog.show();
}
public void showSuccess(String message) {
Alerter.create( this )
.setTitle( "Success" )
.setText( message )
.setBackgroundColorRes( R.color.successAlert )
.show();
}
}
MyFirebaseMessagingService.java
package com.bertho.chat;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived( remoteMessage );
String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get( "from_user_id" );
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder( this )
.setSmallIcon( R.drawable.notif_icon )
.setContentTitle( notificationTitle )
.setContentText( notificationBody );
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra( "user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService( NOTIFICATION_SERVICE );
mNotifyMgr.notify( mNotificationId, mBuilder.build() );
}
}
根据描述错误日志是指这一行
mUserDatabase = FirebaseDatabase.getInstance().getReference().child( "Users" ).child( user_id );
在这种情况下,应用程序是否无法从 Firebase 数据库加载数据以致崩溃?
请指教
最佳答案
我更改了我的 Firebase 函数
const payload = {
notification: {
title: "New Friend Request",
body: `${userName} has sent you Friend Request`,
icon: "default",
click_action: "com.bertho.chat_TARGET_NOTIFICATION"
},
data: {
from_user_id: from_user_id
}
};
到
const payload = {
data: {
title: "New Friend Request",
body: `${userName} has sent you Friend Request`,
from_user_id: from_user_id,
click_action: "com.bertho.chat_TARGET_NOTIFICATION"
}
};
感谢@UmarZaii
关于java - 火力地堡 : App crash when notification comes with app not opened,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388110/
我遵循了提供的文档 here并且可以在 Android N 及更高版本上成功创建通知组。然而,我遇到的问题是 Android Oreo,每个发布到群组的通知都会为群组播放声音。 这很烦人,因为我只想播
单点触控 当应用程序返回前台时,我需要激活的 ViewController 来了解这一点。 是否有一个事件或覆盖我可以用来确定 View 被带到前台。 我确实找到了“WillEnterForegrou
我正在尝试在我的项目中创建一个通知系统。 这些是我已经完成的步骤: 1-php artisan notifications:table 2-php artisan migrate 3-php arti
我想知道如何批量发送推送通知。我们从以下3个主要模式来看问题: 一次API调用->一条消息->发送给指定用户 一次 API 调用 -> 一条消息 -> 传递给许多不同的用户(由 id 指定) 一个 A
我想在用户点击保存或删除按钮时显示通知。 Here is what i use for it.(Kendo notification) 我认为我的 CSS 部分有问题,我更改了很多东西但我仍然在原地。
在我的测试代码中,我使用了 notification.flags |= Notification.DEFAULT_SOUND; notification.flags |= No
当 Redis Sentinel 通知事件时,它不会提供 Redis 主节点的名称。 配置摘录: # sentinel notification-script # # Call the speci
Microsoft Exchange 引入了流通知作为 Exchange 2010 中拉/推通知的替代方法。有关流的基本介绍可以在此 msdn article 上找到。和 blog 但是,我无法弄清楚
我在理解WebExtensions notification.onClicked事件的文档时遇到问题。 最终,我要在单击通知时将通知文本复制到剪贴板。但是,现在我在理解回调内容或必须在其中插入noti
我即将实现ionic-native Push Notifications .这可以在浏览器中运行吗?还是我需要安装 iOS/Android 模拟器? 最佳答案 除非您使用 Phonegap 推送服务器
Safari 12.1 是否支持服务 worker PWA 推送通知?我试过这个 demo在 iOS 上,但它仍然不适合我。 有机会得到它们吗?谢谢。 最佳答案 目前没有关于此功能的通信...Appl
我遇到了一个奇怪的错误。一段时间以来,这种崩溃一直在我的 Fabric 中出现 致命异常:android.app.RemoteServiceException:startForeground 的错误通
我正在使用 Google Cloud Messaging 在 iOS 应用中接收推送通知。为了了解该系统的可靠性,我进行了两项测试。1.当应用程序处于前台时接收通知 下面称为curl 12. 当应用程
String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationM
我想在我的 Android 应用程序中使用 Notification,但我不知道 Android 中的 Notification 和 Push Notification 有什么区别. 我在网上搜索了这
我有两个模型之间的多对多关系: 用户 namespace App\Models; class User extends Model { use HasApiTokens, Notifiable
我在 App Store 中有一个 iPad 应用程序,其逻辑主要依赖于本地通知。换句话说,应用程序内部发生的很多事情都是由委托(delegate)方法应用程序 didReceiveLocalNoti
我知道如何制作每天在特定时间触发的本地通知。但是如何为每个通知更改 UNMutableNotificationContent.Body? 现在它只是一遍又一遍地显示相同的文本。 我正在为 iOS 10
请问,是否可以在没有用户 ID 的情况下发送推送通知?假设我们想向所有下载了我们应用程序的用户发送推送消息。或者出于安全原因,用户 ID 不能存储在数据库中(或在应用程序中使用)。 最佳答案 不管怎样
我在使用 Twilio notify 发送通知时遇到异常。 当我在使用相同的 Twilio NotificationCreator bean 发送 SMS 后发送通知时,如果我发送通知而不发送 SMS
我是一名优秀的程序员,十分优秀!