我正在开发一个Android应用程序,如果他/她处于非活动状态超过3天,我想向用户发送“We Miss You”通知。 这意味着如果用户未打开我们的应用程序3天,我想向他们发送“我们想念你”的通知 像Deer Hunter和Temple Run一样 谢谢:))
答案 0 :(得分:19)
您希望设置每天启动一次短期服务的闹钟。该服务所做的就是检查上次使用时间的共享偏好。如果该时间超过3天,则服务会发送通知。无论哪种方式,服务然后退出。
或者,您的应用程序可以在每次运行时提交警报,该警报定义为在3天内触发,并定义为使用相同的ID替换任何预先存在的警报。该警报将被定义为打开发送通知的短期服务。
这里有一些演示第二种方法的示例代码。如上所述,它仅在启动时更新运行时间。如果您的应用程序长时间运行,您需要定期致电recordRunTime()
。如果用NotificationCompat.Builder替换Notification.Builder对象,则不推荐使用的方法getNotification()
可以替换为build()
。您可以使用delay
的注释掉的行来缩短延迟时间。
package com.example.comeback;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private final static String TAG = "MainActivity";
public final static String PREFS = "PrefsFile";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Save time of run:
settings = getSharedPreferences(PREFS, MODE_PRIVATE);
editor = settings.edit();
// First time running app?
if (!settings.contains("lastRun"))
enableNotification(null);
else
recordRunTime();
Log.v(TAG, "Starting CheckRecentRun service...");
startService(new Intent(this, CheckRecentRun.class));
}
public void recordRunTime() {
editor.putLong("lastRun", System.currentTimeMillis());
editor.commit();
}
public void enableNotification(View v) {
editor.putLong("lastRun", System.currentTimeMillis());
editor.putBoolean("enabled", true);
editor.commit();
Log.v(TAG, "Notifications enabled");
}
public void disableNotification(View v) {
editor.putBoolean("enabled", false);
editor.commit();
Log.v(TAG, "Notifications disabled");
}
}
package com.example.comeback;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
public class CheckRecentRun extends Service {
private final static String TAG = "CheckRecentPlay";
private static Long MILLISECS_PER_DAY = 86400000L;
private static Long MILLISECS_PER_MIN = 60000L;
// private static long delay = MILLISECS_PER_MIN * 3; // 3 minutes (for testing)
private static long delay = MILLISECS_PER_DAY * 3; // 3 days
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "Service started");
SharedPreferences settings = getSharedPreferences(MainActivity.PREFS, MODE_PRIVATE);
// Are notifications enabled?
if (settings.getBoolean("enabled", true)) {
// Is it time for a notification?
if (settings.getLong("lastRun", Long.MAX_VALUE) < System.currentTimeMillis() - delay)
sendNotification();
} else {
Log.i(TAG, "Notifications are disabled");
}
// Set an alarm for the next time this service should run:
setAlarm();
Log.v(TAG, "Service stopped");
stopSelf();
}
public void setAlarm() {
Intent serviceIntent = new Intent(this, CheckRecentRun.class);
PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pi);
Log.v(TAG, "Alarm set");
}
public void sendNotification() {
Intent mainIntent = new Intent(this, MainActivity.class);
@SuppressWarnings("deprecation")
Notification noti = new Notification.Builder(this)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 131314, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("We Miss You!")
.setContentText("Please play our game again soon.")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("We Miss You! Please come back and play our game again soon.")
.setWhen(System.currentTimeMillis())
.getNotification();
NotificationManager notificationManager
= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(131315, noti);
Log.v(TAG, "Notification sent");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="enableNotification"
android:text="@string/enable" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="disableNotification"
android:text="@string/disable" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Come Back</string>
<string name="enable">Enable Notifications</string>
<string name="disable">Disable Notifications</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.comeback"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.comeback.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.comeback.CheckRecentRun" >
</service>
</application>
</manifest>
答案 1 :(得分:0)
你可以拥有类似的东西:
当用户打开应用程序时,您将其记录在您网站上的数据库中,并在每天下午3点运行一个cron作业(或其他等效作业)并且如果用户上次打开日期超过3天前您发送通知。
或强>
当用户打开应用程序时,您将其记录在应用程序的sqlite数据库中,并将警报管理器设置为3天。每次应用程序打开时,警报都会重置。 3天后,您可以创建通知。
答案 2 :(得分:0)
我尝试通过广播制作此功能。这是实现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
package com.example.myapplication
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var sharedPreferences: SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sharedPreferences = applicationContext.getSharedPreferences(MAIN_ACTIVITY_PREFS, Context.MODE_PRIVATE)
recordRunTime()
Log.i("MainActivity", "Starting com.example.myapplication.MyReceiver broadcast...")
sendBroadcast(Intent(this, MyReceiver::class.java))
}
fun recordRunTime() {
val editor = sharedPreferences?.edit()
editor?.putLong("lastRun", System.currentTimeMillis())
editor?.apply()
}
}
package com.example.myapplication
import android.app.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val settings = context.applicationContext.getSharedPreferences(MAIN_ACTIVITY_PREFS, Service.MODE_PRIVATE)
// Are notifications enabled?
if (settings.getBoolean(ENABLED, true)) {
// Is it time for a notification?
val channel = createChannel()
Log.i(TAG, "onReceive: ")
makeStatusNotification("Notification Message", context, channel)
Log.i(TAG, "onStartCommand: " + settings.getLong(LAST_RUN, -1))
setAlarm(context)
} else {
Log.i(TAG, "Notifications are disabled")
}
}
private fun createChannel(): NotificationChannel? {
var channel: NotificationChannel? = null
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val name = VERBOSE_NOTIFICATION_CHANNEL_NAME
val description = VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION
val importance = NotificationManager.IMPORTANCE_HIGH
channel = NotificationChannel(CHANNEL_ID, name, importance)
channel.description = description
}
return channel
}
companion object {
fun setAlarm(context: Context) {
val broadcastIntent = Intent(context, MyReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context.applicationContext, 121314, broadcastIntent,
PendingIntent.FLAG_CANCEL_CURRENT)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent)
Log.i(TAG, "Alarm set")
}
const val TAG = "AlarmReceiver"
private const val MILLISECS_PER_MIN = 10000L
const val delay = MILLISECS_PER_MIN * 1// 0.5 minutes (for testing)
//private const val MILLISECS_PER_DAY = 86400000L
//private const val delay = MILLISECS_PER_DAY * 3 // 3 days
const val VERBOSE_NOTIFICATION_CHANNEL_NAME =
"Check Last Run"
const val VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION =
"Shows notifications if the user didn't open the app for 3 day"
const val NOTIFICATION_TITLE = "Notification Title"
const val CHANNEL_ID = "VERBOSE_NOTIFICATION"
const val NOTIFICATION_ID = 1021
const val ENABLED = "is_enabled"
}
}
package com.example.myapplication
const val MAIN_ACTIVITY_PREFS = "mainPrefs"
const val LAST_RUN = "lastRun"
package com.example.myapplication
import com.example.myapplication.MyReceiver.Companion.CHANNEL_ID
import com.example.myapplication.MyReceiver.Companion.NOTIFICATION_ID
import com.example.myapplication.MyReceiver.Companion.NOTIFICATION_TITLE
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import java.lang.NullPointerException
fun makeStatusNotification(message: String, context: Context, channel: NotificationChannel?) {
// Make a channel if necessary
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Add the channel
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (channel != null)
notificationManager.createNotificationChannel(channel)
else
throw NullPointerException("Notification Channel can't be NULL")
}
// Create the notification
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(LongArray(1))
.setCategory(NotificationCompat.CATEGORY_ALARM)
// Show the notification
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
}
希望这对您有帮助。