如何在接近警报中初始化通知

Posted

技术标签:

【中文标题】如何在接近警报中初始化通知【英文标题】:How to initialize Notification in the Proximity alert 【发布时间】:2016-02-14 18:25:57 【问题描述】:

尝试在用户经过此位置时使用 Proximity Alert 通知用户。我将 resultIntent 分配给创建通知的 Notification_Handler.class,但是当用户靠近该位置时,通知不会被推送,在这种情况下如何推送通知?

//This is android App 
    protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    // resize map
    mapFragment = (SupportMapFragment) (getSupportFragmentManager()
            .findFragmentById(R.id.map));
    ViewGroup.LayoutParams params = mapFragment.getView().getLayoutParams();
    params.height = 1400;
    mapFragment.getView().setLayoutParams(params);
    //Notify if user near a location
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Intent resultIntent = new Intent(this, Notification_Handler.class);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0,          resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    lm.addProximityAlert(21.422, 39.826, 5, -1, pendIntent);

编辑:这是通知处理程序类

    package app.historical_markers.historical_markers;


    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.NotificationCompat;
    import android.support.v4.app.TaskStackBuilder;
    import android.support.v7.app.ActionBarActivity;

    import app.historical_markers.historical_markers.Not_Signed_In.KSA_Map;


    public class Notification_Handler extends ActionBarActivity 
        final int notificationID = 1234;

    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.drawable.icon2);
        mBuilder.setContentTitle("Notification Alert!!!");
        mBuilder.setContentText("Hi, You are near this Marker!");

        Intent resultIntent = new Intent(this, KSA_Map.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(KSA_Map.class);

// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

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

// notificationID allows you to update the notification later on.
        mNotificationManager.notify(notificationID, mBuilder.build());

    

【问题讨论】:

【参考方案1】:

LocationManager.addProximityAlert()中radius参数的单位是米。您指定的值为 5。这太小了。 GPS 不是那么准确,您已将纬度/经度指定为仅约 10 米的精度。

使用更大的半径值,比如 100。创建PendingIntent 的代码是正确的。

【讨论】:

好的,谢谢,我会修改半径,但我主要关心的是如何在接近警报中添加通知而不是启动另一个意图? 接近警报只会导致您指定的活动(在这种情况下为 Notification_Handler)运行。如果要出现通知,则需要在 Notification_Handler 中放入生成它的代码。您是说活动正在运行,您有代码可以发布通知,但通知没有出现?如果是这样,请在 Notification_Handler 中发布生成通知的代码。 我已经编辑了问题并添加了通知处理程序类,它有什么问题吗? 我认为构建通知的代码没有任何问题。在Notification_HandleronCreate() 方法中添加Log 语句,以便确认它正在被调用。 这是documentation 的Log。输出通常出现在 IDE 的窗口中。您也可以将 logcat 命令用作described here。在网上搜索更多示例和教程。

以上是关于如何在接近警报中初始化通知的主要内容,如果未能解决你的问题,请参考以下文章

接近警报不会触发并通知

在 Android 上显示已保存的接近警报?

使用flutter firebase的应用程序内部通知

当我的应用在后台收到本地通知时避免发出警报

如何在 UserDefault 中存储推送通知警报消息?

如何使用 SwiftUI 呈现警报