0

我正在尝试在 Android Oreo API 26 上运行的 android wear 模拟器上接收 FCM 通知。

我已经在 firebase 上正确注册了应用程序,firebase 令牌正在打印在日志上,并且通知通道也在创建中。

我正在使用 firebase 控制台发送消息,但它没有收到磨损。控制台显示 0 条消息已发送,0 条已接收。我正在通过通知通道以相同的名称从控制台发送通知testing_channel

收到衣服需要时间吗?

清单声明:

<service
        android:name=".Services.NotificationInstanceService"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

服务等级:

public class NotificationInstanceService extends FirebaseInstanceIdService{

    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken() ;
        Log.i("TAG", "onTokenRefresh: " + refreshedToken);

    }
}

应用控制器:

public class AppController extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

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


            String id = "testing_channel";
            CharSequence name = "Testing";
            String description = "This channel is primarily for testing";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = null;
            mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mNotificationManager.createNotificationChannel(mChannel);

            Log.i("TAG", "onCreate: NOTIFICATION CHANNEL CREATED" );

        }
    }
}
4

2 回答 2

0

您应该使用当前最新的 AppCompat 库 26.0.2

compile "com.android.support:appcompat-v7:26.0.+"

在 Android O 中,必须在 Notification Builder 中使用通道

下面是一个对我有用的示例代码:

// Sets an ID for the notification, so it can be updated.

int notifyID = 1; String CHANNEL_ID = "my_channel_01";// 频道ID。

NotificationCompat notification =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setChannelId(CHANNEL_ID).build();



NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   mNotificationManager.createNotificationChannel(mChannel);
}

// Issue the notification.


 mNotificationManager.notify(notifyID , notification);

并使用“Google API”(任何版本)目标来接收推送通知

于 2017-12-05T12:07:05.880 回答
0

始终建议在真实设备上测试推送通知。如果模拟器目标使用 Google API 版本来接收推送通知,您可以在模拟器上测试推送通知。尝试使用 Google API 目标编译您的 Android 项目。我认为有必要进行测试。

于 2017-12-05T11:52:26.730 回答