android - 使用 firebase 在 android 中推送通知获取 token

标签 android firebase notifications

我想做推送通知,这是我的代码

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("FCM NOTIFICATION");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon((R.mipmap.ic_launcher));
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }
}

但它不起作用,我认为这个问题与 token 有关我已经看到很多关于获取 token 的问题和答案,但我无法在那里获得有用的信息。我将不胜感激任何答案,在此先感谢。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MainActivity.this,  new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String newToken = instanceIdResult.getToken();
            Log.e("newToken",newToken);

        }
    });
}

最佳答案

试试这个方法。 1. 创建服务

public class YourService extends FirebaseMessagingService {

public static int NOTIFICATION_ID = 1;

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    if (NOTIFICATION_ID > 1073741824) {
        NOTIFICATION_ID = 0;
    }
    Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());
}

现在将其添加到 list

<service
   android:name=".YourService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

关于android - 使用 firebase 在 android 中推送通知获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609283/

相关文章:

cocoa - 一定时间后隐藏 NSUserNotification

android - 关闭应用程序时播放背景音乐

javascript - Cordova 在后台运行 JavaScript 函数

swift - 如何使用 JWT for Google firebase 生成授权 token ?

java - 保持用户登录 Java Firebase

android - 如果有多个通知,如何提供计数器

node.js - Nodewebkit 应用程序的桌面通知

javascript - Phonegap 和 Cordova - 如何删除默认的启动屏幕?

android - 铃声首选项默认为无声,即使设置了默认值

reactjs - 如何使用这些代码将数据放入 MuiDataTable 中?