我想在android API中发送通知
我想我的Gradle文件也有问题。
我尝试添加一个if语句,条件是android.os.Build。版本SDK_INT
进口包装:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
通知代码:
//Create notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Create channel in which to send push notifications
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_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});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
//Send push notification
Notification notify = new Notification.Builder(getApplicationContext())
.setContentTitle("Listener available on pSquared!")
.setContentText("You can now talk about your day in a pSquared chatbox with a Listener")
.setSmallIcon(R.drawable.psquared_logo).setChannelId(CHANNEL_ID).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notify);
Gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.psquared"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.firebaseui:firebase-ui-auth:4.1.0'
implementation 'com.firebaseui:firebase-ui-database:4.3.2'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.firebaseui:firebase-ui:1.2.0'
implementation 'com.firebase:firebase-client-android:2.5.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
//compile 'com.android.support:design:25.0.1'
}
行:
implementation 'com.android.support:appcompat-v7:28.0.0'
有一条红色下划线,带有错误:
所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。发现版本28.0.0, 25.1.1。示例包括:com.android.support:动画矢量绘制:28.0.0和com.android.support:调色板v7:25.1.1少...(Ctrl F1)
我希望通知能够在Android API 18及以上工作
你需要在Api级别上使用NotificationChannel
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Create channel in which to send push notifications
String CHANNEL_ID = "my_channel_01";
// only use NotificationChannel when Api Level >= 26
if(Build.VERSION.SDK_INT >= 26) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_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});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
//Send push notification
Notification notify = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle("Listener available on pSquared!")
.setContentText("You can now talk about your day in a pSquared chatbox with a Listener")
.setSmallIcon(R.drawable.psquared_logo)
.build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notify);
在SDK更新(23)之后,我得到了这个lint错误,我没有对我的代码做任何更改,它在api级别为9的设备上运行良好。另外,我在代码中根本不调用android.app.活动#onCreateView。如果我单击自动修复,它会将@SuppressLint(NewApi)放入类,这样错误就会消失,我想确定这是否是要走的路。
问题内容: 我是JSON的新手。我按照本教程创建了示例应用程序。当我尝试将代码复制到我的日食时,它显示错误。 请指教。谢谢 问题答案: 转到android AndroidManifest.xml 在您的文件中是8 将SDK版本更改为9,如下所示
我的代码中有这样一行: 显示以下错误: 调用需要API级别16(当前最小值为14) 如果API级别较高,它是否限制了我的应用程序可以使用的设备数量?
当应用程序被强制退出时,似乎不会调用函数。我的印象是,无论应用程序处于何种状态,都会调用该函数,但似乎只有在应用程序已经在后台运行时才会调用该函数。如果应用程序尚未使用新的iOS 7远程通知后台模式运行,是否有办法在后台唤醒该应用程序?
我正在使用Firebase(FCM)向用户显示推送通知,但我遇到了一个奇怪的问题。 我的代码适用于以下场景(使用FirebaseMessagingService): 前台应用 - 在 onReceive() 中接收数据并在应用内显示弹出窗口。 后台应用 - 在 onReceive() 中接收数据并为用户显示通知。如果单击此按钮,应用程序将被带回前台。在LauncherActivity中收到此目的的
当按下后退按钮时,我需要最小化应用程序。 我使用下面的代码来捕获硬件后退按钮点击事件 帮助我在后按键时最小化的代码